0

When I hit the enter key I cannot change the focus on the divs. Can anyone help?

<div contenteditable="true" id="t" tabindex="-1">
            <div class="me" spellcheck="true" content="inhert" >Hello <span style="font-weight: normal;">world2!</span></div>
            <div class="me" spellcheck="true" content="inhert" >Hello <span style="font-weight: normal;">world3!</span></div>
            <div class="me" spellcheck="true" content="inhert" id="last">Hello <span style="font-weight: normal;">world4!</span></div>
</div>

$("div").bind("keypress", function(event){
                if(event.keyCode == 13){                            
                    event.preventDefault();
                    $("#last").focus();
                }
            });
Travis Pettry
  • 1,220
  • 1
  • 14
  • 35

3 Answers3

1

Try this:

HTML:

<div contenteditable="true" id="t" tabindex="-1">
    <div class="me" spellcheck="true" content="inhert" >Hello 
        <span style="font-weight: normal;">world2!</span></div>
    <div class="me" spellcheck="true" content="inhert" >Hello 
        <span style="font-weight: normal;">world3!</span></div>       
</div>
<div contenteditable="true" class="me" spellcheck="true" content="inhert" id="last">Hello 
    <span style="font-weight: normal;">world4!</span></div>

Make the last div contenteditable="true" and it should be outside the main div

Script:

$("div").bind("keypress", function(event){
    if(event.keyCode == 13){    
        //alert('ya');
        event.preventDefault();
        $("#last").focus();

    }
});

Fiddle: http://jsfiddle.net/Q4J87/

You can use it like

$("#last").prop('contenteditable',true).focus();
// alternative if contenteditable not set for last div

Fiddle: http://jsfiddle.net/Q4J87/1/

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • its working fine..there is no alternative for adding div with contenteditable property.. – sasi Mar 15 '13 at 05:08
  • Yes without `contenteditable` it can not be possible. Read http://stackoverflow.com/questions/1599660/which-html-elements-can-receive-focus for `focus`, also `focus` will `set` only for `editable elements`. – Rohan Kumar Mar 15 '13 at 05:15
1

Focus does not work on divs by default.

The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (, , etc.) and links (). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

Read

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

The following code should work:

<html>
        <head>
                <title>Test Website</title>
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
                <script>
                        $(document).ready(function() {
                                                $("div#last").focus();
                        });
                </script>
        </head>
<body>

        <div contenteditable="true" id="t" tabindex="1">
                    <div class="me" spellcheck="true" content="inhert" >Hello <span style="font-weight: normal;">world2!</span></div>
                    <div class="me" spellcheck="true" content="inhert" >Hello <span style="font-weight: normal;">world3!</span></div>
                    <div class="me" spellcheck="true" content="inhert" tabindex="2" id="last">Hello <span style="font-weight: normal;">world4!</span></div>
        </div>


</body>

</body>
</html>
Paul Calabro
  • 1,748
  • 1
  • 16
  • 33
  • I think the issue was you weren't including a tabindex on the div you wanted to focus on. As someone alluded to, this is a requirement for all elements that aren't in that limited set of elements that have the ability to gain focus. – Paul Calabro Mar 15 '13 at 05:23