Konstantin Brodin
Konstantin Brodin

Reputation: 142

Get nested element value

i have the html:

<div>
     <label>
        <input type='radio' name='a'>
        <span></span>
        <input type='hidden' value='1'>
     </label>
     <label>...
</div>

I need to get value of hidden input by click event on label.

I have javascript like this:

$('label').live('click', function () {

        var value = $(this).children('input:hidden').val();

    });

But this is not work. Can anybody help?

Upvotes: 1

Views: 1091

Answers (3)

rejo
rejo

Reputation: 3350

 <form action="form_action.asp" method="get">
 Email: <input type="text" name="email" /><br />
<input type="hidden" name="country" value="Norway" />
 <input type="submit" value="Submit" />
   </form>


$(function() {
$('label').on('click', function () {
    var value = $(this).children('input:hidden').val();

});

});

Upvotes: 1

Guillaume Poussel
Guillaume Poussel

Reputation: 9822

First, unless you absolutely have to, you should avoid live() function. Depending on jQuery version, use bind(), click() or on()

But your code works fine, see this Fiddle.

Upvotes: 1

ebaxt
ebaxt

Reputation: 8417

Works fine:

<div>
    <label>
        <input type='radio' name='a'>
        <span></span>
        <input type='hidden' value='1'>
    </label>
</div>
<script type="text/javascript">
    $('label').live('click', function () {  
        var value = $(this).children('input:hidden').val();
        console.log(value);   
    });
</script>

Output: 1

Upvotes: 1

Related Questions