Enhance function to copy values to fields AFTER current field

Rouchie used Ask the Experts™
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();
			if ($(this).val()=='') {

Open in new window

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)

If the textboxes are sibling to each other or at the same level, then you can use index() method for the same
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.

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