tabbing between jeditable fields in a table
Asked Answered
M

1

4

I'm using code from here http://www.korvus.com/blog/geek/making-the-tab-key-work-with-jeditable-fields/ to get tabbing between jeditable fields working, and if the fields are on their own it works fine. However I need to have my fields in a table, and the only time the tab key works is tabbing from the last field to the first, and of course I need it to tab from first to next and so on...

$('div.edit').bind('keydown', function(evt) {
if(evt.keyCode==9) {
    $(this).find("input").blur();
    var nextBox='';

     if ($("div.edit").index(this) == ($("div.edit").length-1)) {
           nextBox=$("div.edit:first");         //last box, go to first
       } else {
            nextBox=$(this).next("div.edit");    //Next box in line
       }
    $(nextBox).click();  //Go to assigned next box
    return false;           //Suppress normal tab
};
});

The table is formatted like this

<table>

<tr>
  <td class='leftcolumn'>
     <strong>Firstname:</strong>
  </td>
  <td>
     <div class='edit' id='firstname'><?=$userdetail['firstname']?></div>
  </td>
</tr>

<tr>
  <td class='leftcolumn'>
     <strong>Lastname:</strong>
  </td>
  <td>
     <div class='edit' id='lastname'><?=$userdetail['lastname']?></div>
  </td>
</tr>
</table>

Thanks in advance

Moan answered 8/10, 2010 at 13:8 Comment(1)
see #886116Sharkey
C
8

I believe the issue is that your input fields are not direct siblings to each other, thus "next()" is failing. I think this will work:

$('div.edit').bind('keydown', function(evt) {
if(evt.keyCode==9) {
    var nextBox='';
    var currentBoxIndex=$("div.edit").index(this);
     if (currentBoxIndex == ($("div.edit").length-1)) {
           nextBox=$("div.edit:first");         //last box, go to first
       } else {
            nextBox=$("div.edit").eq(currentBoxIndex+1);    //Next box in line
       }
    $(this).find("input").blur();
    $(nextBox).click();  //Go to assigned next box
    return false;           //Suppress normal tab
};
}); 
Calcimine answered 20/9, 2011 at 17:22 Comment(1)
This worked for me! Except I need one adjustment...can you help me here, @SylvanK? #24935569Butler

© 2022 - 2024 — McMap. All rights reserved.