George Huang
George Huang

Reputation: 2523

Use jQuery to find an element id that contains a variable

I know I can use jQuery to find html elements whose id contains certain text. like below:

$("div[id*='uStatus']").first();

The above code helps me find all the elements whose ids contain 'uStatus'. However, if the 'uStatus' itself is a variable then it won't work.

I tried the code below, but it returns no result:

var name = 'uStatus' + loginName;  
$("div[id*='" + name + "']").first();

I am sure the login name is there. Thanks a lot for your help !

Below is the attached the rendered HTML for reference:

<div id="ContentPlaceHolder1_peopleList1_gvPeople_uStatusUserB_0"><div style='padding:4px; background-color:#dedede;font-size:11px;'>UserB</div></div>
<div id="ContentPlaceHolder1_peopleList1_gvPeople_uStatusUserA_1"><div style='padding:4px; background-color:#dedede;font-size:11px;'>UserA</div></div>

Upvotes: 0

Views: 2543

Answers (1)

coco
coco

Reputation: 88

Try to remove the single quote from the jQuery selector. Hope this helps.

var name = 'uStaus' + loginName;  
$("div[id*=" + name + "]").first();

Upvotes: 1

Related Questions