最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - two divs same class, get title only of THIS div - Stack Overflow

matteradmin5PV0评论

I thought this would be easy but I'm stuck and can't find a solution. I have two divs, both same class. The difference is the content inside each one. I'm trying to get the text and values from each but the output is from both of them. I have an image and when it's clicked, I want to get the value from THIS div where the image is located. Here's my code:

<div class="mainSelect">
    <h1>I am Title</h1><br>
    <a href="javascript:void(0);" class="clickme"><img class="pic" src="img/img.png"/></a>
</div>
<div class="mainSelect">
    <h1>I am Second Title</h1><br>
    <a href="javascript:void(0);" class="clickme"><img class="pic" src="img/img.png"/></a>
</div>

And in javascript:

$(document).ready(function(){
    $(".clickme").click(function(){
        var t = $("h1").text();
        alert (t);
    });
});

When I do it this way, I get "I am Title I am Second Title". Is there a global way to only display the title of the div where the image was clicked?

I thought this would be easy but I'm stuck and can't find a solution. I have two divs, both same class. The difference is the content inside each one. I'm trying to get the text and values from each but the output is from both of them. I have an image and when it's clicked, I want to get the value from THIS div where the image is located. Here's my code:

<div class="mainSelect">
    <h1>I am Title</h1><br>
    <a href="javascript:void(0);" class="clickme"><img class="pic" src="img/img.png"/></a>
</div>
<div class="mainSelect">
    <h1>I am Second Title</h1><br>
    <a href="javascript:void(0);" class="clickme"><img class="pic" src="img/img.png"/></a>
</div>

And in javascript:

$(document).ready(function(){
    $(".clickme").click(function(){
        var t = $("h1").text();
        alert (t);
    });
});

When I do it this way, I get "I am Title I am Second Title". Is there a global way to only display the title of the div where the image was clicked?

Share Improve this question asked Apr 30, 2013 at 14:41 user2025469user2025469 1,5812 gold badges14 silver badges26 bronze badges 6
  • 1 As a side remark, you shouldn't have multiple h1 tags in the same page, that's not semantically correct. – Laurent S. Commented Apr 30, 2013 at 14:43
  • 2 you don't need a br after h1 since it's a block element (if you need spacing use CSS) – David Fregoli Commented Apr 30, 2013 at 14:46
  • 1 also, you might wanna change href to # and return false at the end of the click handler. – David Fregoli Commented Apr 30, 2013 at 14:52
  • 1 @user2025469 stackoverflow./questions/134845/… and stackoverflow./questions/3498492/… and stackoverflow./questions/5237105/… – Ian Commented Apr 30, 2013 at 14:57
  • 1 If the a doesn't really have the purpose and semantic value of an anchor you can simply remove it and attach the clickme class and handler to the img itself and it will prevent the hash being appended to the url. cursor: pointer CSS on it will display the hand cursor – David Fregoli Commented Apr 30, 2013 at 15:07
 |  Show 1 more ment

3 Answers 3

Reset to default 5

You need to use this:

var t = $(this).siblings('h1').text();

$(document).ready(function(){
    $(".clickme").click(function(){
        var t = $(this).closest(".mainSelect").find("h1").text();
        alert (t);
    });
});

use this selector, then find h1 in its parent.

 $(".clickme").click(function(){
        var t = $(this).closest('.mainSelect').children('h1').text();
        alert (t);
 });
Post a comment

comment list (0)

  1. No comments so far