-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
  • 4,689
  • 8
  • 43
  • 83
Devesh Agrawal
  • 7,964
  • 16
  • 67
  • 118

2 Answers2

2

Omit the closest - all you need is the find

$topcat.find(':checkbox').val();
tymeJV
  • 99,730
  • 13
  • 150
  • 152
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
  • 19,992
  • 16
  • 88
  • 107