Aba Aba - 2 years ago 199
Javascript Question

jQuery .next() not returning next element

I'm trying to use jQuery's .next() function to get the next element with

but all I'm getting is

If you click on the empty cell the input element is displayed (function click_td(td_id)/ open_input(inputObj)).

When exiting the element it disappears leaving the text (function close_input(inputObj)).

By pressing tab I want to move to the next element (function next_cell(inputObj)).


<script language="javascript" type="text/javascript" src="/_inc/jquery/jquery.js"></script>
<style type="text/css">
display: none;
<table cellspacing="0" cellpadding="2" border="1" width="50%">
<td width="15%">Name</td>
<td width="35%" onclick="click_td(this)" id="1_1"><span id="si1_1" style=""></span><input type="text" onblur="close_input(this);" onkeydown="if (event.keyCode==13) return false;if (event.keyCode==9) return next_cell(this)" size="25" value="" name="name" id="i1_1" class="inputfield"></td>
<td width="15%">E-mail</td>
<td width="35%" onclick="click_td(this)" id="1_2"><span id="si1_2"></span><input type="text" onblur="close_input(this);" onkeydown="if (event.keyCode==13) return false;if (event.keyCode==9) return next_cell(this)" size="25" value="" name="email" id="i1_2" class="inputfield"></td>
<td onclick="click_td(this)" id="2_1"><span id="si2_1"></span><input type="text" onblur="close_input(this);" onkeydown="if (event.keyCode==13) return false;if (event.keyCode==9) return next_cell(this)" size="25" value="" name="phone" id="i2_1" class="inputfield"></td>
</td><td onclick="click_td(this)" id="2_2"><span id="si2_2"></span>
<select onblur="close_input(this);" onkeydown="if (event.keyCode==13) return false;if (event.keyCode==9) return next_cell(this)" name="article_type" id="i2_2" class="inputfield">
<option value=""></option>
<option>Buying Guide</option>
<option>Hands-on Review</option>

<script LANGUAGE="JavaScript">
function click_td(td_id){
var inputObj = document.getElementById('i' + td_id.id);

function open_input(inputObj){
var spanObj = document.getElementById('s' + inputObj.id)
inputObj.style.display = 'block';
spanObj.style.display = 'none';

function close_input(inputObj){
var spanObj = document.getElementById('s' + inputObj.id)
switch (inputObj.type){
case 'select':
spanObj.innerHTML = inputObj.options[inputObj.selectedIndex].value;
case 'text':
spanObj.innerHTML = inputObj.value;
inputObj.style.display = 'none';
spanObj.style.display = '';

function next_cell(inputObj){
next_inputObj = $(inputObj).next('.inputfield');

Answer Source

I've finished work on my fiddle to highlight the problem


var next_inputObj = $(inputObj).next('.inputfield');

Essentially, it boils down to the .next() operator not being able to find the class you've suggested. This is because the .next() operator will look for the next item with the class "inputField" in the that you're in at that point which, of course, doesn't exist so the .next() operator always returns an empty array.

As an aside, the HTML you posted is missing an opening tag, but that's not the issue here. If you chain together some .parent() operators, you can get the correct level and go from there e.g.

var next_inputObj = $(inputObj).parent().next('.inputfield');

EDIT: It's pretty much there now; you can tab between the two boxes at the top and the two at the bottom. It requires some extra code to navigate to the next when you've reached the last element in the list.

One of the issues was that pressing tab fires a blur event which was affecting the ability to tab between inputs on the page. I'd recommend preventing this functionality by calling the preventDefault() method on the event as below. To implement this, I used the jQuery event handlers as you were replicating code in your inputs - documentation found here: http://api.jquery.com/blur/ and http://api.jquery.com/keydown/


This should now provide a working solution, hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download