Solved

How do I find the next input element in a HTML table using jQuery?

Posted on 2014-10-05
2
1,099 Views
Last Modified: 2014-10-05
Hi experts! I'm looking to find the next input element in table using jQuery, or even just plain javascript.

I'd like to select the next input when the focus is received on an input. It needs to find the next input even if the next <td> doesn't have one, and/or if the next one is in the next <tr>, it should find it.

For example, in the following example, when the input with id "input2" receives the focus, I need to select "input3".

<table>
	<tr>
		<td><input type="text" id="input1"></td>
		<td><input type="text" id="input2"></td>
		<td>no input here</td>
	</tr>
	<tr>
		<td><div class="random"><input type="text" id="input3"></div></td>
		<td>no input here</td>
		<td><input type="text"  id="input4"></td>
	</tr>
</table>

Open in new window


Any help with coming up with efficient code for accomplishing this is appreciated!
0
Comment
Question by:Drevo
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40362925
test page : http://jsfiddle.net/hsasLzsb/1/

jQuery(function() {
    $("table :text").focus(function() {
        var myIndex = $(this).index("table :text");
        var nextTextbox = $("table :text:eq(" + (myIndex + 1) + ")");
        if( nextTextbox.length == 0 ) nextTextbox = $("table :text:eq(0)");
        // checking
        alert( nextTextbox.attr("id") );
    });
});

Open in new window

0
 

Author Closing Comment

by:Drevo
ID: 40363016
This works great! Thanks for going through the extra  effort of putting together a jsfiddle too! I really appreciate it!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question