Jump to content
funbinod

remove elements not in for loop range

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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 by dsonesuk

Share this post


Link to post
Share on other sites

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)

  • Thanks 1

Share this post


Link to post
Share on other sites
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 by funbinod

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×