Hulk
Hulk

Reputation: 34180

jquery find command

Using .find() how to find whether radio button exists in a div or else raise an alert

          <div id="emp_form">

          </div>

Upvotes: 0

Views: 617

Answers (3)

Alpesh
Alpesh

Reputation: 5405

Try this -

if ($('#emp_form :radio').length != 0) {
    alert('exists');
  } else {
    alert('does not exist');
  }

Upvotes: 1

sathis
sathis

Reputation: 547

if ( $("#emp_form").find("input[type='radio']").length >0 ) {

} else {
   alert("There is nothing");
}

Fixed.

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630559

You can use .find() (or just a descendant selector) and check the .length like this:

if($("#emp_form :radio").length == 0) {
  alert("No radio buttons found!, Crap!");
}

Of if you want to do something in the case there are radio buttons:

if($("#emp_form :radio").length > 0) {
  //do something
} else {
  alert("No radio buttons found!, Crap!");
}

The .find() alternative is $("#emp_form").find(":radio").length.

Upvotes: 2

Related Questions