Rajeev
Rajeev

Reputation: 46919

jquery remove check box on condition

In a div there are these check boxes(name="val1") and after a certain operation these check boxes are removed

<div name="navigation_b">
    <label id="selectall">
        select all 
        <input type="checkbox" name="selectall" />
    </label>
    <input type="checkbox" name="val1" />   
    <input type="checkbox" name="val1" />   
    <input type="checkbox" name="val1" />   
    <input type="checkbox" name="val1" />   
    <input type="checkbox" name="val1" />   
</div>

If all the checkboxes(name =val1) are removed then the selectall should not be visible.How to do this using jquery

Upvotes: 0

Views: 1649

Answers (3)

sharat87
sharat87

Reputation: 7526

If your question is to auto-show the select all checkbox, when the others are removed, I don't think that is possible with jQuery, unless there is an event that jQuery sports which would fire when an element is removed. You'll have to show the select all checkbox manually may be by using one of the fine solutions provided in the other answers.

Upvotes: 0

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29166

if( $("input:checkbox[name='val1']").length==0)
{
       $("input:checkbox[name='selectall']")
      .hide();
}

Source is here, and here

Edit

You could hide the entire label which contains selectall -

if( $("input:checkbox[name='val1']").length==0)
{
       $("label#selectall")
      .hide();
}

Upvotes: 1

griegs
griegs

Reputation: 22760

You could check to see if there are any of the checkboxes and if not then remove the label

if($('input:checkbox[name="val1"]').length) {
    // do something
}
else {
    // do something else
}

the above is untested but i think it's pretty close.

Upvotes: 0

Related Questions