How can I find a li element which has data-rel attribute and its value "a1" ?
<li class="thumbImg" data-rel="a1">
<img src="img/a0.jpg" width='150' height="80">
</li>
var relValue = "a1";
$('li[data-rel=relValue]');
How can I find a li element which has data-rel attribute and its value "a1" ?
<li class="thumbImg" data-rel="a1">
<img src="img/a0.jpg" width='150' height="80">
</li>
var relValue = "a1";
$('li[data-rel=relValue]');
String concatenation:
var relValue = "a1";
$('li[data-rel="' + relValue + '"]');
Or in ES2015 and higher, a template literal:
var relValue = "a1";
$(`li[data-rel="${relValue}"]`);
// Note --^--------------------------^---- those are backticks
The inner quotes ("
in the above) are optional for values that are valid CSS identifiers (like a1
), but a good idea in case the value may not be (for instance, a 1
).