Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get checkbox value from a specific li

I have the following html structure. I can select the the <li> but cannot get the checkbox value - it's undefined

<ul>
    <li class="selected" style="false">
        <label class="">
            <input type="checkbox" data-name="selectItem" value="2447"> 
            <span>England</span>
        </label>
    </li>
</ul>
$('ul li.selected').each(function () {
    console.log('li.selected');
    var val = $(this).closest('label').find('[type=checkbox]').val();
    console.log(val);                                        
});
like image 568
Eyal Avatar asked Mar 08 '26 05:03

Eyal


1 Answers

closest() goes up the DOM, you should use find() as you want to go down instead. Also note that you can achieve what you require in a single call to find(). Try this:

$('ul li.selected').each(function () {
    var val = $(this).find('label :checkbox').val();
    console.log(val);                                        
});
like image 173
Rory McCrossan Avatar answered Mar 09 '26 23:03

Rory McCrossan