Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give <a> element autofocus

With the following code I try to give an a element autofocus when the page is loaded.

<a href="/{{setting}}" id="next" class="button big active">Next vocabulary</a>
<script>
    document.getElementbyId("next").focus();
</script>

Whilst this works fine with input fields it doesn't work with that a element. Can anybody explain me while?

Regards

like image 460
orschiro Avatar asked Mar 05 '12 11:03

orschiro


People also ask

How do I autofocus a div in HTML?

The autofocus attribute is a boolean attribute. When present, it specifies that the element should automatically get focus when the page loads.

How does autofocus work in HTML?

The autofocus attribute is a boolean attribute. When present, it specifies that an <input> element should automatically get focus when the page loads.

How do I create an autofocus button in HTML?

The autofocus property sets or returns whether a button should automatically get focus when the page loads, or not. This property reflects the HTML autofocus attribute. Note: The autofocus attribute is new for the <button> element in HTML5.


1 Answers

The problem is that you typed getElementById wrong, it should be an uppercase 'B'.

<a href="/{{setting}}" id="next" class="button big active">Next vocabulary</a>
<script>
    document.getElementById("next").focus();
</script>

This should now work!

like image 166
Johan Lindskogen Avatar answered Sep 25 '22 08:09

Johan Lindskogen