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");
}
$("r_t").remove();should be$("div[name=r_t]").remove();$("r_n").html('');should be$("#r_n").html('');$("r_n").appendshould be$("#r_n").appendand$("r_n :checkbox").lengthshould be$("#r_n :checkbox").lengthfinally you have an extra
{after theif..`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)