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?

2
  • String concatenation. Given $("[data-uid=uid]"), how could it possibly know which if any uid should use the variable? Commented Aug 16, 2014 at 5:32
  • 1
    This question was answered here: stackoverflow.com/questions/4191386/… Commented Aug 16, 2014 at 5:34

2 Answers 2

4

Try this instead:

$("[data-uid=" + uid + "]")
Sign up to request clarification or add additional context in comments.

Comments

3

Use + to do so.

JS:

 var uid_var = 4;
 $("[data-uid="+ uid_var +"]")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.