views:

36

answers:

3

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

          <div id="emp_form">

          </div>
+2  A: 

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.

Nick Craver
Thanks Nic.........
Hulk
A: 
if ( $("#emp_form").find("input[type='radio']").length >0 ) {

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

Fixed.

sathis
Your attribute selector needs to be right next to `input`, a space denotes a descendant, so this is currently looking for a `type="radio"` element *inside* an `<input>` which won't exist :)
Nick Craver
+1  A: 

Try this -

if ($('#emp_form :radio').length != 0) {
    alert('exists');
  } else {
    alert('does not exist');
  }
Alpesh
This *does* need a space between selectors, the radio elements don't *have* that ID, they're *inside* the element with that ID.
Nick Craver
Thanks nick ( radio button are children of div#emo_form ), I forgot to add a space.
Alpesh