Solved

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

Posted on 2014-10-05
2
1,016 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
Comment Utility
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
Comment Utility
This works great! Thanks for going through the extra  effort of putting together a jsfiddle too! I really appreciate it!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
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…

772 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

9 Experts available now in Live!

Get 1:1 Help Now