?
Solved

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

Posted on 2014-10-05
2
Medium Priority
?
1,248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
Suggested Courses

777 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