troubleshooting Question

Possible to streamline a function for reading DOM indexes

Avatar of Rouchie
RouchieFlag for United Kingdom of Great Britain and Northern Ireland asked on
8 Comments1 Solution233 ViewsLast Modified:
My page has multiple tables each with many text input fields.  In the first row of each table is a DELETE link, which, when clicked, clears the values of all text inputs from that point onwards (not text inputs in earlier tables).  It works really well, but I am not sure if it is a waste of resources:

<a onclick="ClearFields('txt_someID', this); return false;" href="#">Delete</a>
function ClearFields(idToClear, startCtrl){
	var parentTable = $(startCtrl).closest('table'); // find the parent table
	var startIndex = $(parentTable).index('*');  // find the DOM index of the parent table
	$("input[type='text'][id$='" + idToClear  + "']").each(function(){ // for each text input on the entire page
		if ($(this).index('*') > startIndex) { // if the text input DOM index is greater than the parent table
			$(this).val(''); // clear the value

My question relates to the fact that if my page had 500 text fields, and the Delete link was clicked next to the 200th text box, the JQuery loop would unecessarily have to check the index value of the previous 200 text fields BEFORE it started to clear the remaining fields.  It seems a real waste of resources.  Is there a way to streamline this to trim the array of elements to start at the desired index (startIndex)?  I've looked at :gt() and slice() but these only affect the returned array after the search takes place.
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros