Motive
Motive

Reputation: 3111

Selecting hidden field value in same div in jQuery

This should be simple, not sure why I'm having issues.

I've tried $this.closest('.image_id').val() but I keep getting undefined.

Here's the jQuery I'm using:

$('#image_container').on('change', '[name="image-type"]', function() {

    $this = $(this);

    console.log($this.closest('.image_id').val());

});

The HTML:

<div id="image_library"> 
    <div class="row-fluid library-row">

        <div class="span3 library-file-name">
          file name
        </div>

        <div class="span1 library-image-size">
            file size
        </div>

        <div class="span4 library-actions">

            <input type="hidden" class="image_id" name="image_id" value="1303">

            <select name="image-type">
                <option value="none">Not Set</option>
                <option value="thumbnail">Thumbnail</option>
                <option value="feature">Feature</option>
                <option value="gallery">Gallery</option>
            </select>

        </div>
    </div>
</div>

Upvotes: 0

Views: 1968

Answers (2)

Ayman Gado
Ayman Gado

Reputation: 36

​$(document).ready(function(​){
   $('[name="image-type"]').on('change', function() {
       $this = $(this);
       console.log($this.siblings('.image_id').val());
    }); 
});​

Upvotes: 0

jantimon
jantimon

Reputation: 38180

Closest searchs for a parent but you are looking for a sibling. This can be done with the following code:

$(this).siblings('.image_id').val()

http://api.jquery.com/siblings/

Upvotes: 4

Related Questions