Hi Friends, I'm working on a small task which is to enable the user to tabindex the html element upon enter keypress.
As im new to jquery , I have written some code which seems to me that It will work ,but there are some problems in it.
Initial findings
The culprit code ,it doesnt work ,as the ouput in the Msg lablel is "Undefined"
$('*').attr('tabindex').id
The code is given below and I have even created a JSFiddle.
JQuery
$(document).ready(function (eOuter) {
$('input').bind('keypress', function (eInner) {
if (eInner.keyCode == 13) //if its a enter key
{
var tabindex = $(this).attr('tabindex');
tabindex++; //increment tabindex
//after increment of tabindex ,make the next element focus
$('*').attr('tabindex', tabindex).focus();
**//Msg Label**
//Just to print some msgs to see everything is working
$('#Msg').text( this.id + " tabindex: " + tabindex
+ " next element: " + $('*').attr('tabindex').id);
return false; // to cancel out Onenter page postback in asp.net
}
});
}
);
HTML
<div>
Employee Info<br />
Name<br />
<input name="TxtbxName" type="text" value="ok" id="TxtbxName" tabindex="1" />
<br />
Age<br />
<input name="TxtbxAge" type="text" id="TxtbxAge" tabindex="2" />
<br />
Gender<br />
<select name="DdlGender" id="DdlGender" tabindex="3">
<option selected="selected" value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br />
<div>
Previous Employment<br />
<select name="DdlCompany" id="DdlCompany" tabindex="4">
<option selected="selected" value="0">Folio3</option>
<option value="1">Null Soft</option>
<option value="2">Object Soft</option>
<option value="3">Excepption Soft</option>
</select>
or Enter Code
<input name="TxtbxCompanyCode" type="text" id="TxtbxCompanyCode" tabindex="5" />
<br />
Address<br />
<input name="TxtbxAddress" type="text" id="TxtbxAddress" tabindex="6" />
<br />
<input type="submit" name="BtnSubmit" value="Submit" id="BtnSubmit" tabindex="7"/>
<br />
<label id="Msg">Message here</label>
</div>
</div>
Let me know where I went wrong :/
I found a couple of minor jQuery issues. Fixed here: JSFiddle.
This line:
can be written like this:
and this:
is not calling the id attribute the jQuery way (you are using JavaScript syntax, but the result of $(this) is a jQuery object. So...
$(this).id
becomes$(this).attr('id')
.The form still has a submission problem, that I didn't dig too far into, but it changes focus and fills out the '#Msg' element now.
Didn't want to make new post and make SPAM with solution I found useful.
Collected information from other sources (Brian Glaz code nice-one) and made cross-browser version of Focus Next Element In Tab-index using Enter key.
Tab-indexes are not one after another, but may also contain a space between (1,2,9,11,30,etc.)
I hope that someone will find it useful. Feel free to edit/comment it.
Editable cells in dynamic table
Seeking for a solution for exactly this problem, I generated a small jquery function to find the next valid tabindex; suppose it's a bit easier to maintenance and maybe a bit faster than a while loop:
Hope this to be helpful to whoever needs it; this is tested and works.
Explaning this in short: seek for the element of the current tabindex, find this element in the list of all tabindex elements, get the index of it and increase the index.
Then, using this function, you may select the next tabindex element that way:
I didn't test the call but suppose it to work.
Here is my full working code to
focusNextElement
onkeydown
[Enter] without jQuery with JSFiddle example created based on the following stackoverflow answers: