-3

How do I, with the clearest syntax, find the second element with the data-id 411 ?

<div data-id="410">Yo</div>
<div data-id="411">No</div>
<div data-id="412">Mm</div>
Kunj
  • 1,980
  • 2
  • 22
  • 34
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232

2 Answers2

3

$("div[data-id=411]").text("Hello")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div data-id="410">Yo</div>
<div data-id="411">No</div>
<div data-id="412">Mm</div>

Something as simple as this should work. Jason is right though, the jQuery website has some great documentation.

Shriike
  • 1,351
  • 9
  • 20
1

You want to assign a class name to the elements you are searching from, so that it's not looking at the whole document.

<div class="search">
<div data-id="410">Yo</div>
<div data-id="411">No</div>
<div data-id="412">Mm</div>
</div>

var element = $('.search').find("div[data-id='411']");
guest
  • 2,185
  • 3
  • 23
  • 46