Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  194] [ 6]  / answers: 1 / hits: 15461  / 13 Years ago, sat, december 24, 2011, 12:00:00

I have this code:



<div class=item>
<div class=hidden>44</div>
<input type=submit id=btnAddCommentForAnswer value=Add Comment />
<script type=text/javascript>
$(document).ready(function () {
$('#btnAddCommentForAnswer').click(function () {
alert(XXX);
});

});
</script>
</div>

<div class=item>
<div class=hidden>12</div>
<input type=submit id=btnAddCommentForAnswer value=Add Comment />
<script type=text/javascript>
$(document).ready(function () {
$('#btnAddCommentForAnswer').click(function () {
alert(XXX)
});

});
</script>
</div>


what code should I put at XXX to get the content of the div with class=hidden when i press the button at the same div with class=item?


If you click the first button you should get 44, and for clicking the second button you get 12.


More From » jquery

 Answers
4

id's are meant to be unique; you may wish to consider changing the id to a class.
With that in place, you could use a script like the following to achieve what you want:



$(document).ready(function() {
$('.btnAddCommentForAnswer').click(function() {
alert($(this).siblings('.hidden').text());
});
});


Here's a JSFiddle example: JSFiddle



Also, you don't need to have two script tags in your script! This one script wrapped in <script> tags is all that is necessary :)


[#88394] Thursday, December 22, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarod

Total Points: 62
Total Questions: 111
Total Answers: 83

Location: Saint Vincent and the Grenadines
Member since Sat, Sep 11, 2021
3 Years ago
jarod questions
;