Upon page load I want to move the cursor to a particular field. No problem. But I also need to select and highlight the default value that is placed in that text field.
Upon page load I want to move the cursor to a particular field. No problem. But I also need to select and highlight the default value that is placed in that text field.
From http://www.codeave.com/javascript/code.asp?u_log=7004:
var input = document.getElementById('myTextInput');
input.focus();
input.select();
<input id="myTextInput" value="Hello world!" />
To do it on page load:
window.onload = function () {
var input = document.getElementById('myTextInput');
input.focus();
input.select();
}
<input id="myTextInput" value="Hello world!" />
var input = document.getElementById('myTextInput');
input.focus();
input.setSelectionRange( 6, 19 );
<input id="myTextInput" value="Hello default value world!" />
select particular text on textfield
Also you can use like
input.selectionStart = 6;
input.selectionEnd = 19;
Using the autofocus attribute works well with text input and checkboxes.
<input type="text" name="foo" value="boo" autofocus="autofocus"> FooBoo
<input type="checkbox" name="foo" value="boo" autofocus="autofocus"> FooBoo
Let the input text field automatically get focus when the page loads:
<form action="/action_page.php">
<input type="text" id="fname" name="fname" autofocus>
<input type="submit">
</form>
Source : https://www.w3schools.com/tags/att_input_autofocus.asp