Enhance function to copy values to fields AFTER current field

Rouchie
Rouchie used Ask the Experts™
on
The following function copies values from a text box to all text boxes on the page that have a particular ID value.  It works well, but I was wondering if it was possible to enhance the logic so that instead of copying the value to ALL textboxes that match the given ID value, to just copy values to all matching text boxes that appear in the DOM after startBox?

function CopyValues(startBox){
		var startboxid = '#' + startBox;
		var level = $(startboxid).val();
		$("input[type='text'][id$='txt_Level']").each(function(){
			if ($(this).val()=='') {
					$(this).val(level);
			}
		});
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you can easily check the matching id using any of the selectors (startswith, endWith, contains etc)
http://api.jquery.com/category/selectors/attribute-selectors/

If the textboxes are sibling to each other or at the same level, then you can use index() method for the same
http://api.jquery.com/index/
Okay thanks.  How do I read the index value of an element relative to the entire DOM rather than a parent element?  The examples shown seem to read the index value of a list item relative to the list, rather than the entire DOM.

Thanks
I think I've figured it out.. Can you please advise that this is correct...

var startIndex = $("#myElementID").index('*');

Open in new window

Yes, it seems correct.
Did you tried the same? does it give correct output?
Yes it works thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial