Reputation: 4041
I am trying to get the value of a hidden input type, but it's not working.
JQUERY (in script tags):
$('.flagComment').click(function() {
var commentid = $(this).siblings('.commentId').attr('value');
alert(commentid);
});
HTML (this echos several times in a while loop):
<div class='bar'>
<a href='#' class='flagComment'>Flag</a>
</div>
<input type='hidden' class='commentId' value='testvalue' />
Upvotes: 1
Views: 1843
Reputation: 1943
input is not a sibling for this link. Now it works:
<div class='bar'>
<a href='#' class='flagComment'>Flag</a>
<input type='hidden' class='commentId' value='testvalue' />
</div>
You can try also with closest() function:
$('.flagComment').click(function() {
var commentid = $(this).closest('.commentId').attr('value');
alert(commentid);
});
Upvotes: 0
Reputation: 342765
Try:
var commentid = $(this).parent().next('.commentId').val();
since the hidden input is a sibling of the div, and not the anchor.
Upvotes: 3