How do you automatically set the focus to a textbox when a web page loads?
Is there an HTML tag to do it or does it have to be done via Javascript?
Advertisement
Answer
If you’re using jquery:
JavaScript
x
4
1
$(function() {
2
$("#Box1").focus();
3
});
4
or prototype:
JavaScript
1
4
1
Event.observe(window, 'load', function() {
2
$("Box1").focus();
3
});
4
or plain javascript:
JavaScript
1
4
1
window.onload = function() {
2
document.getElementById("Box1").focus();
3
};
4
though keep in mind that this will replace other on load handlers, so look up addLoadEvent() in google for a safe way to append onload handlers rather than replacing.