3

I am trying to get a function which searches and selects the following content of a division which is contenteditable="true".

The HTML looks like this:

<table>
    <tr>
        <td>Headline:</td>
        <td><div class="editableDiv" contenteditable="true"></td>
    </tr>
    <tr>
        <td>Headline:</td>
        <td><div class="editableDiv" contenteditable="true"></td>
    </tr>
</table>

I think the code should look like this but I probably used the next() selector in a wrong way:

$('table tr td .editableRow').click(function(){

$(this).next().dblclick();

});
cHao
  • 84,970
  • 20
  • 145
  • 172
Niczem Olaske
  • 271
  • 1
  • 4
  • 13

1 Answers1

6

Listen for keydown on the editable element and check for character code 9 - this is the tab key. If so, move to the next item:

var $editableDiv = $('.editableDiv');
$editableDiv.keydown(function(e){
    if ( e.which === 9 ) {
        var $index = $editableDiv.index(this),
            $next = $editableDiv.eq($index+1);
        if ( $next.length ) {
            $next.focus();
        } else {
            $editableDiv.first().focus(); // cycle
        }
        e.preventDefault();
    }
});

Here's a fiddle: http://jsfiddle.net/rhEC3/2/

My example also cycles to the first .editableDiv if it has reached the end, if you don't want this, move the e.preventDefault() to only within the next focus block and remove the cycle block, fiddle: http://jsfiddle.net/rhEC3/3/

Also, somewhat related: Find next closest focusable element using jquery?

Community
  • 1
  • 1
zamnuts
  • 9,492
  • 3
  • 39
  • 46