2

Suppose I have something like this

<div class="xx" data-id="tid">Jumbo Jet</div>
<div class="press"> clickme </div>
<div class="foo">
   ....
</div> 
<div class="xx" data-id="tid">Hello World</div>
<div class="xx" data-id="tid">Red Brown Fox</div>

Is there a jquery function that would search under it (not its child) for a specific id and return back the first result. In this case I should get "Hello World". Will the find method work ? I am doing something like this

$(".press").click(function () {
    var item = $(this).find("#tid");
    //Get the classname
    var name = item.attr('class'); //returns undefined should return xx
    //Also how do I return the content ? "Hello World"
    console.log(name); 
});

Any suggestions on what I might be doing wrong here ?

8
  • how about using the next() method? api.jquery.com/next I assume you could do var item = $(this).next() Commented Sep 9, 2019 at 0:57
  • 3
    An ID should be unique within a page Commented Sep 9, 2019 at 0:57
  • 4
    The problem is that you have duplicate IDs in the first place, which is invalid HTML. I'd suggest changing those to classes instead, assuming you have access to the HTML. Commented Sep 9, 2019 at 0:57
  • Like the others already said: ID's should always be unique! There's simply no excuse not to do this. If using classes isn't an option for you, you can use data- attributes instead. Commented Sep 9, 2019 at 1:04
  • next fails if there are other items after the press button Commented Sep 9, 2019 at 1:36

3 Answers 3

1

Considering:

The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).

update: you could use nextAll() to find under the current element and get element by his data attribute and filter the first match.

You could try this:

$(".press").click(function () {
    var item = $(this).nextAll('[data-id="tid"]:first')
    var name = item.attr('class')    
    var content = item.html() // html() get the html content
    console.log('class name:', name); 
    console.log('content:', content); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="xx" data-id="tid">Jumbo Jet</div>
<div class="press"> clickme </div>
<div class="foo">
   ....
</div> 
<div class="xx" data-id="tid">Hello World</div>
<div class="xx" data-id="tid">Red Brown Fox</div>

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to use something like an id but not have the conflicts, I would recommend using data-id.

$(".press").on("click", function(){

  var content = "";
  content = $(this).next("[data-id='tid']").html();

  console.log(content);
  
});
.press:hover {
  cursor: pointer;
  background: orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<br><hr><br>

<div class="xx" data-id="tid">Jumbo Jet</div>
<div class="press"> clickme </div>
<div class="xx" data-id="tid">Hello World</div>
<div class="xx" data-id="tid">Red Brown Fox</div>
<br><hr><br>
<div class="xx" data-id="tid">Jumbo Jet 2</div>
<div class="press"> clickme </div>
<div class="xx" data-id="tid">Hello World 2</div>
<div class="xx" data-id="tid">Red Brown Fox 2</div>

2 Comments

This fails if there are other classes right after the press button
I am not sure what you mean about it failing. I have put more classes after this as a test and as you can see it chooses the next occurrence of the data-id="tid". What part is failing for you specifically?
0

id property should be unique, not Repetitious. You can use a class with the same name.

The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). https://www.w3schools.com/html/html_id.asp

$(".press").click(function () {
  var item = $(this).next(".xx");
  //Get the classname
  var name = item.attr('class'); //returns undefined should return xx
  //Also how do I return the content ? "Hello World"
  console.log(name); 

  console.log(item.text()); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.