views:

57

answers:

1

In the following code,after removing the checkboxes and adding it again.The alert always becomes true for "could not find checkboxes"

<div id="r_n">   
    <div name="r_t"><input type="checkbox" name="r_name" />Name</div>
    <div name="r_t"><input type="checkbox" name="r_name" />Address</div>
    <div name="r_t"><input type="checkbox" name="r_name" />Value</div>
    <div name="r_t"><input type="checkbox" name="r_name" />Total</div>
</div>

 <script>
    $("r_t").remove();
    $("r_n").html('');

Now all the checkboxes are removed form the dom

$("r_n").append('<div name="r_t"><input type="checkbox" name="r_name" />Name</div>
             <div name="r_t"><input type="checkbox" name="r_name" />Address</div>
             <div name="r_t"><input type="checkbox" name="r_name" />Value</div>
             <div name="r_t"><input type="checkbox" name="r_name" />Total</div>');

if($("r_n :checkbox").length > 0) {
  {
         alert("Could not find checkboxes")
  }
  else
  {
      alert("Found");
  }   
+4  A: 

$("r_t").remove(); should be $("div[name=r_t]").remove();
$("r_n").html(''); should be $("#r_n").html('');
$("r_n").append should be $("#r_n").append and
$("r_n :checkbox").length should be $("#r_n :checkbox").length

finally you have an extra { after the if ..` Does it work with those changes ?

UPDATE

Finally your logic is wrong..

you say if the length is > then 0 (means that it found at least 1 checkbox) then show "Could not find checkboxes", but it should really be if length is == 0 (length of 0 means not checkboxes found)

Gaby
@Gaby:Points taken.After removing and re adding it.It is always alerts in if(length == 0)
Rajeev
@Rajeev, have a look at a complete code example at http://jsfiddle.net/MPzw5/
Gaby
@gaby : thanks.
Rajeev