1

I'm trying to do a filter that when a button pressed it will show only the row has checked input in the table.

<table class="table">
    <tr>
        <th>Name</th>
        <th>InUse</th>
        <th></th>
    </tr>
    <tr>
        <td>foo 1</td>
        <td>
            <input checked="checked" class="check-box" disabled="disabled" type="checkbox" />
        </td>
        <td>
            <a href="/AdditivesNames/Edit/5">Edit</a> |
            <a href="/AdditivesNames/Details/5">Details</a> |
            <a href="/AdditivesNames/Delete/5">Delete</a>
        </td>
    </tr>
    <tr>
        <td>foo 2</td>
        <td>
            <input checked="checked" class="check-box" disabled="disabled" type="checkbox" />
        </td>
        <td>
            <a href="/AdditivesNames/Edit/3">Edit</a> |
            <a href="/AdditivesNames/Details/3">Details</a> |
            <a href="/AdditivesNames/Delete/3">Delete</a>
        </td>
    <tr/>
    <!-- not checked -->
    <tr>
        <td>foo 3</td>
        <td>
            <input class="check-box" disabled="disabled" type="checkbox" />
        </td>
        <td>
            <a href="/AdditivesNames/Edit/5">Edit</a> |
            <a href="/AdditivesNames/Details/5">Details</a> |
            <a href="/AdditivesNames/Delete/5">Delete</a>
        </td>
    </tr>
</table>
0

4 Answers 4

2

You can use :has selector or .has() method to checking existence of element in parent.

$("button").click(function(){
    $("table tr").has(".check-box:not(:checked)").hide();
});
table, tr, td {
    border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>
        <th>Name</th>
        <th>InUse</th>
    </tr>
    <tr>
        <td>foo 1</td>
        <td>
            <input class="check-box" type="checkbox" checked disabled  />
        </td>
    </tr>
    <tr>
        <td>foo 2</td>                  
        <td>
            <input class="check-box" type="checkbox" disabled  />
        </td>
    </tr>
    <tr>
        <td>foo 3</td>                  
        <td>
            <input class="check-box" type="checkbox" checked disabled  />
        </td>
    </tr>
    <tr>
        <td>foo 4</td>                  
        <td>
            <input class="check-box" type="checkbox" disabled  />
        </td>
    </tr>
</table>
<button>Only checked</button>

You can test code on full of you html in demo

Sign up to request clarification or add additional context in comments.

3 Comments

@Yogi_Bear What is meaning of doen't work? Do you checked jsfiddle? It Both of code work in demo
no, I tried it on my own web app, and it didnt work. I guess it's because of the asp.net
@Yogi_Bear Use $(document).on("click", "button", function(){}) instead
0

Please use following jquery code. Onclick of submit button all the rows with unchecked checkboxes will be hide.

$(document).ready(function(){
    $("input[type=submit]").click(function () {
        $("input:not(:checked)").each(function () {
            $(this).closest('tr').hide();           
        });
    });
});

1 Comment

i want to disable a table row which contain a checkbox if its unchecked how do i disable it
0

Please use jquery and place this code in your tag

  $(document).ready(function () {
    $("#btnClick").on("click", function () {
        $(".table tr").each(function () {
            if (!$(this).find("td input:checkbox").is(":checked")) {
                $(this).hide();
            }
        })
    })
});

This should work with your case.

1 Comment

@Anburaj_N can you show me in html? I'll help you, if still not fixed
0

This is a much simpler approach. Pass this to the onclick() event handler in your HTML and use jQuery:

<input type="checkbox" onClick="disablerow(this);">

function disablerow(el) {
    if (el.checked == true) {
        $(el).closest('tr').find(":input:text(:first)").attr('disabled', false);
    }
    else {
        $(el).closest('tr').find(":input:text(:first)").attr('disabled', true);
    }     
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.