funbinod Posted September 19, 2018 Share Posted September 19, 2018 hello all! i was trying to do a for loop to remove all elements that does not meet the for loop. following is the code i tried. $("#feetable input").each(function(e) { var thisvalue = $(this).val(); var enclass = 3; var i; for (i=enclass; i<=12; i+=enclass) { if ($(this).parent("td").attr("class") == i && $(this).val() == thisvalue) { $(this).remove(); } } }) and here is the html <td class='1'><input type='checkbox' /></td> <td class='2'><input type='checkbox' /></td> <td class='3'><input type='checkbox' /></td> <td class='4'><input type='checkbox' /></td> <td class='5'><input type='checkbox' /></td> <td class='6'><input type='checkbox' /></td> <td class='7'><input type='checkbox' /></td> <td class='8'><input type='checkbox' /></td> <td class='9'><input type='checkbox' /></td> <td class='10'><input type='checkbox' /></td> <td class='11'><input type='checkbox' /></td> <td class='12'><input type='checkbox' /></td> this removes the exact elemetns that the for loop finds (i.e. inputs under <td>s with class 3, 6, 9, 12). but my aim is to keep those inputs and remove others. and when i try to reverse the code as below it removes all the inputs. if ($(this).parent("td").attr("class") != i && $(this).val() == thisvalue) { $(this).remove(); } please guide me. thanks in advance. Link to comment Share on other sites More sharing options...
dsonesuk Posted September 19, 2018 Share Posted September 19, 2018 (edited) First targets specific class value and will not remove any other than those class values elements Second will target all but the specific class value, on the next loop it falls under the same trap, as 3 is not equal to 6. NOTE: $(this).val() == thisvalue will always be true! they are retrieving the same value from same input Edited September 19, 2018 by dsonesuk Link to comment Share on other sites More sharing options...
dsonesuk Posted September 19, 2018 Share Posted September 19, 2018 The only way to show or remove using == or != is to place those values in an array ShowRemoveArray=[3,6,9,12] (use current for loop to push these values into this array), then use jQuerys, $.inArray() to identify if $(this).parent("td").attr("class") value that exists, is equal '==' array or not equal '!=' to -1 (not found returned value) 1 Link to comment Share on other sites More sharing options...
funbinod Posted September 19, 2018 Author Share Posted September 19, 2018 (edited) 4 hours ago, dsonesuk said: The only way to show or remove using == or != is to place those values in an array ShowRemoveArray=[3,6,9,12] (use current for loop to push these values into this array), then use jQuerys, $.inArray() to identify if $(this).parent("td").attr("class") value that exists, is equal '==' array or not equal '!=' to -1 (not found returned value) thank u. this suggestion solved the problem var remove = []; for (i=enclass; i<=12; i+=enclass) { remove.push(i); } if ($.inArray(parseInt($(this).parent("td").attr("class")), remove) == -1) { $(this).remove(); } Edited September 19, 2018 by funbinod Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now