Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Focus on input field

The HTML on the page has 20 <input> fields each named and given ID's in increasing order from 1 to 20.

If the variable id is set to the next sequential id (id + 1), this function will cause focus to apply to that field. However, when clicking outside of the current input field, the last one input field will not regain focus if the number entered is greater than 10, but an alert will be displayed.

$(":input").focusout(function(){
    var input = $(this).val();
    var id    = $(this).attr('id');
    if(input > 10){ 
        alert('You must enter a number between 0 and 10 '+id);
        $("#"+id).select();
    }
});

How can the last input field be set to regain focus?

like image 693
Somk Avatar asked Aug 12 '11 20:08

Somk


People also ask

How can input focus in jQuery?

Using jQuery With jQuery, you can use the . focus() method to trigger the “focus” JavaScript event on an element. This method is a shortcut for . trigger("focus") method.

How do you set focus on input field?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How do you focus on the input field when the page is loaded?

The autofocus attribute is a boolean attribute. When present, it specifies that an <input> element should automatically get focus when the page loads.

How do you focus text input?

Input Text focus() MethodThe focus() method is used to give focus to a text field. Tip: Use the blur() method to remove focus from a text field.


2 Answers

Try replacing:

$("#"+id).select();

With:

$(this).focus();

In this case, $("#"+id) and $(this) are the same element, and I'm assuming you want to focus the element when there is an error.

Aside: I don't believe that id or name values can legally start with a number, you may want to prefix them with something like option1, option2, etc. It might work, but it might also cause issues later that are difficult to debug. Best to err on the side of caution and best practices.

What are valid values for the id attribute in HTML?

Edit: After failing to get focus() to work, I tried with setTimeout and was able to make it happen. I'm not sure why, or if this is really necessary, but it seems to work.

$(":input").focusout(function(){
    var $this = $(this),
        input = $this.val();

    if (input > 10){
        alert('You must enter a number between 0 and 10');
        setTimeout(function(){
        $this.focus();
        }, 1); 
    }
}); 

I'd love to hear if there is a better way to do this or an explanation. I suspect that the blur and focus events are not fired in an order that makes the previous method possible?

Demo: http://jsfiddle.net/zRWV4/1/

As mentioned in the comments, you should eventually make sure the value is an integer with parseInt or similar (you seem to already be aware of this).

like image 75
Wesley Murch Avatar answered Oct 17 '22 17:10

Wesley Murch


replace

$("#"+id).select();

by

$("#"+id).focus();

or even by

$(this).focus();
like image 29
pixeline Avatar answered Oct 17 '22 18:10

pixeline