1

I can select an HTML5 element by data-* element with:

$("[data_value='5']")

However, I'd like to replace the 5 with a jQuery variable. When I do, I am no longer able to select the element. For example:

> $("[data-uid='4']")
[<div class=​"eventInfo individual" data-uid=​"4">​…​</div>​]
> var uid = 4
> $("[data-uid=uid]")
[]

How do I make a selection using a variable?

Eric Baldwin
  • 3,341
  • 10
  • 31
  • 71
  • String concatenation. Given `$("[data-uid=uid]")`, how could it possibly know which if any `uid` should use the variable? – cookie monster Aug 16 '14 at 05:32
  • 1
    This question was answered here: http://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value – Eric Baldwin Aug 16 '14 at 05:34

2 Answers2

4

Try this instead:

$("[data-uid=" + uid + "]")
Scott Mielcarski
  • 760
  • 6
  • 17
3

Use + to do so.

JS:

 var uid_var = 4;
 $("[data-uid="+ uid_var +"]")
karan3112
  • 1,867
  • 14
  • 20