1

I am using JQuery, and would like to hide rows in a table depending on the value of one of its cells.

I have the following table:

<tr class="tr-class">
  <td class="status">
    <span class="status_submitted">submitted</span>
  </td>
</tr>
<tr class="tr-class">
  <td class="status">
    <span class="status_approved">approved</span>
  </td>
</tr>

I also have the following functions, where I can hide/show the <tr>.

function showSubmitted() {
     $(".tr-class").show();
}
function hideSubmitted() {
     $(".tr-class").hide();
}

But this hides/shows all the rows.

Question

How do I conditionally hide/show the rows?

For example, function hideSubmitted() should just hide the rows where the status cell is submitted.

JSFiddle https://jsfiddle.net/31c90zbv/1/

2
  • You are using class = tr-class which overlooks all other id's . You need to use something more paticuar for every particular row. Commented Jul 28, 2020 at 7:22
  • @Richard > $(".status_submitted").parent().parent().hide(); should work. Commented Jul 28, 2020 at 7:24

4 Answers 4

3

From your question: For example, function hideSubmitted() should just hide the rows where the status cell is submitted.

For this you can change your code like it

  function hideSubmitted() {
    $('.status_submitted').closest(".tr-class").hide();
  }

this will find the closest tr-class to submitted one and hide that one only.

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

Comments

2

I think you are looking for text search and the answer is Jquery contains:

function showSubmitted() {
   $(".tr-class:contains(submitted)").show();
}
function hideSubmitted() {
   $(".tr-class:contains(submitted)").hide();
}

Comments

0

Using plain javascript , you could do some thing like :

function toggle_by_class(cls, on) {
    var lst = document.getElementsByClassName(cls);
    for(var i = 0; i < lst.length; ++i) {
        lst[i].style.display = on ? '' : 'none';
    }
}

Comments

0

You can simply use:

function showSubmitted() {
    $(".tr-class").each(function(){
        if($(this).find('.status_submitted').length > 0) $(this).show();
    });
}

function hideSubmitted() {
    $(".tr-class").each(function(){
        if($(this).find('.status_submitted').length > 0) $(this).hide();
    });
}

And if you want to reduc your code you can do:

function showTr(type) {
    $(".tr-class").each(function(){
        if($(this).find('.status_'+type).length > 0) $(this).show();
    });
}

function hideTr(type) {
    $(".tr-class").each(function(){
        if($(this).find('.status_'+type).length > 0) $(this).hide();
    });
}

showTr('approved');
showTr('submitted');

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.