2

Given n html elements with data attributes such :

<div class="tpl" data-points="2"></div>
<div class="tpl" data-points="5"></div>
<div class="tpl" data-points="10"></div>
<div class="tpl" data-points="5"></div>

Given a JS variable such as :

foo=10;

How to select the element with data-points==foo; ? (which currently is 10 but may change)

What is the suitable selector ?

Hugolpz
  • 16,007
  • 25
  • 91
  • 183
  • Who reopened this and why? It's a duplicate of http://stackoverflow.com/q/6131119/218196, or the ones I mention below. – Felix Kling Jun 20 '14 at 20:44
  • The question contains two parts: [Selecting an element by attribute](http://stackoverflow.com/questions/2487747/selecting-element-by-data-attribute) and [using a variable in a selector](http://stackoverflow.com/q/5891840/218196). – Felix Kling Jun 20 '14 at 20:47

1 Answers1

2
$('div.tpl[data-points=' + foo + ']')

jsFiddle example

j08691
  • 197,815
  • 30
  • 248
  • 265