-1

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]');

1 Answers1

2

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).

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769