Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

I have a web form with a text box in it. How do I go about setting focus to the text box by default?

Something like this:

<body onload='setFocusToTextBox()'>

so can anybody help me with it? I don't know how to set focus to the text box with JavaScript.

<script>

function setFocusToTextBox(){

//What to do here

}

</script>

1 Answer

0 votes
by (40.7k points)

Try the code given below, If your element is something like this:

<input type="text" id="mytext"/>

Your script will be like this:

<script>

function setFocusToTextBox(){

    document.getElementById("mytext").focus();

}

</script>

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...