Jquery focus on load

How to set focus on html element?

I am using document.getElementById('ID_HTML_wanted').focus(); but my html element "ID_HTML_wanted" is not the focus. I am a user jquery api . Focus

+6
source share
2 answers

Try wrapping your code in this code so that it runs AFTER the DOM is ready

 $(function(){ //your code }); 

so that he becomes

 $(function(){ document.getElementById('ID_HTML_wanted').focus(); }); 

However, your element does not have a .focus () method, if you want REALLY to use jQuery one, use

 $(function(){ $("#ID_HTML_wanted").focus(); }); 
+10
source

Sorry, I really can't set the focus when the DOM is ready:

 $( document ).ready(function() { $("#ID_HTML_wanted").focus(); }); 

All three of the following syntaxes . ready () are equivalent:

 $(document).ready(handler) $().ready(handler) (this is not recommended) $(handler) 
+2
source

Source: https://habr.com/ru/post/899564/


All Articles