-1

I have an HTML element:

<a style='text-decoration:none;' id='$innerdata[0]' class='cat' href='#'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='vehicle' value='Bike'>&nbsp;$innerdata[1]</a>

I am trying to get the value of checkbox, but alert() is printing undefined.

$('body').on('click', '.cat', function() 
{
    $topcat = $(this);
    alert ($topcat.closest().find('[type=checkbox]').val());
}

How do I get the value of the checkbox?

alex
  • 6,818
  • 9
  • 52
  • 103
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131

2 Answers2

2

Omit the closest - all you need is the find

$topcat.find(':checkbox').val();
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0
$('input[type=checkbox][name=vehicle]').val()     // general method using jQuery

You should add some unique id or class, otherwise selecting the right input might be tricky. See:

// Selects first checkbox with name `asd`
$('input[type=checkbox][name=asd]:eq(0)').val()   // jQuery
$('input[type=checkbox][name=asd]')[0].value      // jQuery + DOM
Qwerty
  • 29,062
  • 22
  • 108
  • 136