0

I'm trying to hide elements in a table by having a jQuery function run through each row, checking a row's class and hiding based on that. The problem I seem to be running into is the function will check the first row's class and base all other checks to hide on if that first check was true or false. Below is the function and the portion of the form

The form

<select id="select" onchange="eligibility_changed(this);" class="fieldsel">

     <option value="Select One:">Select One:</option>
     <option value=".ineligible">Ineligible</option>
     <option value=".eligible">Eligible</option>
     <option value=".confirmed">Eligible</option>

</select>

And the function

function eligibility_changed($this) {

    jQuery(".fieldsel").each(function() {
        var col = this.id;
        var val = this.value;

        console.log(val);

        if (val != "Select One:") {
            jQuery("#tbl>tbody>tr").each(function() {

                //Ran to check the row's class.
                console.log(jQuery('#eligfld').attr('class'));

                if (!jQuery( "#eligfld" ).is(val)) {
                    jQuery(this).hide();
                }
            });
        }
    });

};
3
  • 1
    ID tags should be unique. If you have multiple elements with the same ID #eligfld then your jQuery will hide all elements with the same ID. Try also the jQuery: $('#eligfld ' + $this.val()).hide() Commented Jul 23, 2015 at 18:17
  • In my experience jQuery only acts on the first element with a particular ID. Commented Jul 23, 2015 at 18:17
  • Need to see table html Commented Jul 23, 2015 at 18:34

1 Answer 1

1

Assuming that the rows have the class you are showing in the options you don't need to loop all the rows just hide that class

if (val != "Select One:") {
     // show all, filter for class and hide that class
     jQuery("#tbl>tbody>tr").show().filter(val).hide();
}

This is ID independent so repeating ID's should not be an issue unless you have multiple tables with same ID

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

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.