Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
195
rated 0 times [  198] [ 3]  / answers: 1 / hits: 30013  / 13 Years ago, tue, october 18, 2011, 12:00:00

How to set the focus on a html element ?



I use document.getElementById('ID_HTML_wanted').focus(); but my html element ID_HTML_wanted as not the focus. I user jquery api .focus


More From » jquery

 Answers
21

Try to wrap your code to this code so it executes AFTER DOM is ready



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


so it will become



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


However, your element't don't have .focus() method, if you want to REALLY use jQuery's one, use



$(function(){
$(#ID_HTML_wanted).focus();
});

[#89546] Monday, October 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breonnamayah

Total Points: 574
Total Questions: 115
Total Answers: 96

Location: England
Member since Sun, May 21, 2023
1 Year ago
;