Autofocus Attribute:
It is a boolean attribute used with input fields, button that set focus on that element on which autofocus attribute has been used when a page is loaded. But we can focus only one element at a time using this attribute.
Syntax:
<input type="text" value=" " name=" " autofocus>
<button type="button" autofocus>
Example 1:
<!DOCTYPE html>
<html>
<body>
<form>
<p>First name: <input type="text" name="first"></p>
<p>Last name: <input type="text" name="last"></p>
<p><button type="button" autofocus>Click Me!</button></p>
</form>
</body>
</html>
It will focus on button when page is loaded.
Example 2:
<!DOCTYPE html>
<html>
<body>
<form>
<p>First name: <input type="text" name="fname" class="focus" autofocus></p>
<p>Last name: <input type="text" name="lname"></p>
<p><input type="submit"></p>
</form>
</body>
</html>
It wiil focus on input field with class="focus" when page will load.
0 Comment(s)