Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to focus input, and deselect text inside it

I am currently wanting to focus the first input element, without selecting the preset value text inside the input element. How can I focus my input element, and then deselect the text inside it? My current focus jQuery is as follows.

$(document).ready(function() {
    $("input[name='title']").focus();
});
like image 536
Solo Avatar asked Jul 09 '12 22:07

Solo


People also ask

How do you clear the input field in text?

To clear an input field after submitting:When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.

How do you focus an input box in HTML?

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.

How do you remove focus from text field?

Use the blur() method to remove the focus from an element, e.g. input. blur() . If you need to remove the focus from the currently active element without selecting it, call the blur method on the activeElement property - document.


1 Answers

You can use the selectionStart and selectionEnd properties:

$(document).ready(function() {
    $("input[name='title']").each(function(){
        this.focus();
        this.selectionEnd = this.selectionStart;
    });
});
like image 149
Danilo Valente Avatar answered Oct 19 '22 23:10

Danilo Valente