Solved

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

Posted on 2014-10-05
2
1,065 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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)

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now