Search code examples
jquerysiblings

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' />

Solution

  • Try:

    var commentid = $(this).parent().next('.commentId').val();
    

    since the hidden input is a sibling of the div, and not the anchor.