-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
  • 5,636
  • 9
  • 47
  • 97
Devesh Agrawal
  • 8,532
  • 16
  • 74
  • 123

2 Answers2

2

Omit the closest - all you need is the find

$topcat.find(':checkbox').val();
tymeJV
  • 102,126
  • 13
  • 159
  • 155
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
  • 24,727
  • 20
  • 99
  • 119