-1

I have a variable called counter, which represents the id of an element.

How would I select this variable's value e.g. the div with the id, which is the value of the variable, in jQuery?

counter = counter + 1;
$('<a> </a>').hover(  function() {
    $(#/*id is the value of variable counter*/).
}
Weafs.py
  • 21,559
  • 8
  • 48
  • 73
CodeFanatic
  • 11,364
  • 1
  • 16
  • 37

1 Answers1

3
$("#"+variable)

That's it! You can combine it even different way, because as mentioned in comments it shouldn't be number.

$("#element_"+variable)

etc.


Or by attribute

$("a[data-value="+counter+"])

And HTML part

<a data-value="1"></a>
Piotr
  • 641
  • 6
  • 17