Homer_J
Homer_J

Reputation: 3323

jQuery Javascript add variable to name

Have the following code:

var i = 1;
while (i<22){ 
  $('[id^="trloc"]').show();
  i++;
}

I need to add the value of i to the id - something like this:

$('[id^="trloc" + i]').show();

But I cannot seem to get it to work - any suggestions?

Upvotes: 0

Views: 36

Answers (2)

Alex
Alex

Reputation: 1593

Try this

 $('#trloc' + i).show();

If you search by id, use '#'. It is both easier and faster.

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

$('[id^="trloc'+ i  +'"').show();

simply

$('#trloc' + i).show();

if you don't have any other part after i like: trloc1_a,troloc2_b etc then second solution will work just fine.

Upvotes: 2

Related Questions