Thursday, February 26, 2009

Javascript - Selecting a text box on page Load

If you want to have a text input box on your page highlighted on first load, you can use the following java script.

insert into the section of your HTML document:

<script language="javascript" type="text/javascript">
function selectThis() {
var selectval = document.getElementById("textboxid");
selectval.select(); }
</script>

insert into the head tag:
<body onload="selectThis();">

This will select a text box on page load, eg.:
<input type="text" id="textboxid" value="foo">

Also if you would like to have the text highligted as soon as the box is clicked, you can use the tag:
onclick:this.select();

full link:
<input type="text" id="textboxid" value="foo" onclick="this.select();">

No comments:

Post a Comment