Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery select NEXT text field on Enter Key Press

I have made a page using jquery, and on load it selects the first text field automatically. I want it to then move to the next field when the ENTER key is pressed.

$('.barcodeField input').bind('keyup', function(event) {
    if(event.keyCode==13){
       $("this + input").focus();     
    }
});

I can't find anything that works on the net. And I've scoured the forums.

like image 503
Grant Unwin Avatar asked Jan 10 '11 17:01

Grant Unwin


People also ask

How do I move focus on next field when Enter is pressed in jQuery?

They both use a custom jQuery selector that I add called :focusable to select all focusable element (including links): // register jQuery extension jQuery. extend(jQuery. expr[':'], { focusable: function (el, index, selector) { return $(el).is('a, button, :input, [tabindex]'); } }); $(document).

How do you activate the next input field on Enter?

Pressing enter key should set focus to next text or numeric input field (product code or quantity), skipping buttons.

How do you check if keypress is enter?

In plain JavaScript, you can use the EventTarget. addEventListener() method to listen for keyup event. When it occurs, check the keyCode 's value to see if an Enter key is pressed.

How do you trigger a button on a Enter key?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.


3 Answers

I've created a little function which can do what you need. This is the version I use so you may need to change the class names but you should get the idea.

<script type="text/javascript">
$(document).ready(function(){
$(".vertical").keypress(function(event) {
        if(event.keyCode == 13) { 
        textboxes = $("input.vertical");
        debugger;
        currentBoxNumber = textboxes.index(this);
        if (textboxes[currentBoxNumber + 1] != null) {
            nextBox = textboxes[currentBoxNumber + 1]
            nextBox.focus();
            nextBox.select();
            event.preventDefault();
            return false 
            }
        }
    });
})
</script>

So basically:-

  • Get all the input fields matching .vertical
  • Find which is the current text box
  • Find the next one
  • Set the focus on that one
like image 159
Martin Parry Avatar answered Oct 21 '22 16:10

Martin Parry


You should use:

$(this).next('input').focus();  
like image 40
Sarfraz Avatar answered Oct 21 '22 17:10

Sarfraz


try this:

(function($){
    $.fn.enterNext = function(){
       var _i =0;
       $('input[type=text], select',this)
            .each(function(index){
            _i = index;
            $(this)
                .addClass('tab'+index)
                .keydown(function(event){
                    if(event.keyCode==13){
                        $('.tab'+(index+1)).focus();
                        event.preventDefault();
                    }
                });

        })
     $( "input[type=submit]",this ).addClass('tab'+(_i+1));
}})(jQuery);

for use:

$('form.element').enterNext();

in my case this is the best solution in that I got because the function .next() is strict with elements outside their branch DOM. The best way is to force an index.

and sorry for my bad English...

like image 41
penacho123 Avatar answered Oct 21 '22 17:10

penacho123