Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

395
Views
Why does jquery recognizes an element stored as a string in a variable when using .prop(), but not when using .text()?

I stored an Id of a in a variable,variantId = $(this).prop('id') and noticed 2 different behaviors of Jquery: When I changed the prop of the <td> - jquery recognized the element stored in the variable although it was stored as a string,

let variantId;
$(document).on('click', ".tbl-itemClass", function () {
    variantId = $(this).prop('id');
    $(variantId).prop('contenteditable', true);//changed the prop

}).

but when I tried getting the value of the <span> nested in the <td>, using .text(), jquery only recognized the element stored in the variable when '#' was added

    on('input', function () {
            let newItemName = $('#' + variantId).find('span.item-description').text();
            //let newItemName = $(variantId).find('span.item-description').text(); //this line returned an empty string instead of the <span>s value.
        })

this is the full code:

let variantId;
        $(document).on('click', ".tbl-itemClass", function () {
            variantId = $(this).prop('id');
            $(variantId).prop('contenteditable', true);

        }).
            on('input', function () {
                let newItemName = $('#' + variantId).find('span.item-description').text();
                //let newItemName = $(variantId).find('span.item-description').text();
            });

This is my .csHtml (don't think it makes a difference, I'm using razor page, Asp.Net)

<tbody>
@foreach (var item in Model)
{
    <tr currentitemid=" @item.Id">
        <td class="tbl-itemClass desc" id="@("description"+ item.Id)" varianttype="@ViewBag.VariantType" >
            <span class="spnSpc">&nbsp;&nbsp;&nbsp;</span>
            <span style="width:90%; padding:1px 5px 1px 2px;" **class="item-description"** contenteditable> @item.Description </span>
        </td>
        
        <td class="tbl-itemClass ">//more code here
        </td>
    </tr>
}

Can you explain the difference please? Thanks!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In jQuery, if you want to select attribute by ID you need to put:

$('#yourId')...

As you are getting variantId stored and used as id, to access elements .text() you need # as id selector.

about 4 years ago · Juan Pablo Isaza Report

0

The answer to this question is that this line

    $(variantId).prop('contenteditable', true);//changed the prop

did NOT work, and the reason why you can mistaken and think it works is because it has the contenteditable attribute in the html element.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!