Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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 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…

734 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