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();
});
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.
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.
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.
You can use the selectionStart
and selectionEnd
properties:
$(document).ready(function() {
$("input[name='title']").each(function(){
this.focus();
this.selectionEnd = this.selectionStart;
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With