Defense
Defense

Reputation: 259

Get checkbox value with jQuery

First I alert this, and alert 'undefined':

alert($("input[name='searchManagers']:checked").val());

But when I try this, why alert 'no':

if($("input[name='searchManagers']:checked").val() == 'undefined')
    alert('yes')
else
    alert('no')

That fix my problem:

if(typeof $("input[name='searchManagers']:checked").val() == 'undefined')
    alert('yes')
else
    alert('no')

Upvotes: 1

Views: 140

Answers (6)

Arun Manjhi
Arun Manjhi

Reputation: 173

Please make some changes and find the output.

  if ($("input[name='searchManagers']").is(':checked'))
  alert('yes');
  else
  alert('no');

Upvotes: 0

Mustafa Genç
Mustafa Genç

Reputation: 2579

Use:

$("input[name='searchManagers']").is(":checked")

Upvotes: 2

Prathap
Prathap

Reputation: 727

Please make sure set the id and name property in check box. And try below the code.

if(!$("input[name='searchManagers']:checked").val()){
    alert('Plase select the searchManagers');
    return false;
}

Upvotes: 0

Om3ga
Om3ga

Reputation: 32823

change 'undefined' to undefined without quotes

if($("input[name='searchManagers']:checked").val() == undefined)

Upvotes: 1

DeadAlready
DeadAlready

Reputation: 3008

Because undefined != 'undefined' If you were to modify your code

  if($("input[name='searchManagers']:checked").val() == undefined)
    alert('yes')
  else
    alert('no')

Then it should work

Upvotes: 1

Brett Zamir
Brett Zamir

Reputation: 14345

The second one you are comparing to a string instead of the undefined type.

Try this:

if($("input[name='searchManagers']:checked").val() == undefined)

or:

if(typeof $("input[name='searchManagers']:checked").val() == 'undefined')

Upvotes: 3

Related Questions