To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.
When showing a form that contains a TextBox, it's common courtesy to focus the TextBox so that the user can begin typing immediately. To focus a TextBox when a Windows Form first loads, simply set the TabIndex for the TextBox to zero (or the lowest TabIndex for any Control on the Form).
The hasFocus() method returns a true if the document (or any element in the document) has focus.
I currently have a textbox in my aspx:
<input type="text" id="myTextbox" value="" />
I was wondering if I could set the focus (put my cursor in that text box) every time my JavaScript method is called. I was hoping it would work along the lines of this:
function setFocus() {
document.getElementById("myTextbox").Focus();
}
Any suggestions?
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