Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select value of adjacent hidden input with Jquery?

When a user clicks on the img at the end of the following "li", I want to get the value of the hidden input two elements previous.

<ul id="theLists">
    <li>
    <input class="checkbox" type="checkbox" name="row"/>
    <input class="contentId" type="hidden" value="64" name="id"/>
    <div id="64" class="editable">Peanuts for me</div>
    <img src="/img/delete.gif" alt="delete"/>
    </li>
</ul>

I tried this:

$(document).ready(function(){
    $("#theLists img").click(function(){
        var contentId = $(this).closest("input[name='id']").val();          
    });

To no avail. Any Ideas? I need to set a var with the row id, which in this case is "64".

like image 480
kevtrout Avatar asked Jun 20 '09 18:06

kevtrout


1 Answers

closest(...) gets the closest parent node, not the closest sibling.

There are, in fact, multiple ways to do what you want.

Try these:

var contentId = $(this).siblings("input[name='id']").val();

var contentId = $(this).parent().children("input[name='id']").val();

var contentId = $(this).prev().prev().val();
like image 185
Jeff Meatball Yang Avatar answered Oct 01 '22 06:10

Jeff Meatball Yang