kirby
kirby

Reputation: 4041

Jquery get input hidden value not working

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

Answers (3)

kaz
kaz

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

karim79
karim79

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

Joe
Joe

Reputation: 82654

This should work better, because .commentId is not a sibling of .flagComment:

$('.flagComment').click(function(){
    var commentid = $(this).parent().next('.commentId').val();
    alert(commentid);
});

Demo

Upvotes: 0

Related Questions