-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]');
user6920839
  • 57
  • 1
  • 5

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
  • 1,031,962
  • 187
  • 1,923
  • 1,875