Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  143] [ 1]  / answers: 1 / hits: 22158  / 9 Years ago, wed, may 13, 2015, 12:00:00

I have below code:



<ul id='someId'>
<li class='someClass'>
</li>
</ul>


I want to set focus on first li element within ul based on some condition.



My first attempt is like this:



var ul = document.getElementById('someId');           
var child = ul.childNodes[0];
child.focus();


My second attempt is like this :



var y = document.getElementsByClassName('someClass');
var aNode = y[0];
aNode.focus();


But none of the above works



Any ideas?


More From » html

 Answers
6

The problem is that you can't focus a non input element without setting tabIndex.



<li tabIndex=-1>...</li>


You can Try this fiddle: jsfiddle


[#66626] Sunday, May 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myakylas

Total Points: 66
Total Questions: 85
Total Answers: 95

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
myakylas questions
Thu, Apr 28, 22, 00:00, 2 Years ago
Thu, Apr 8, 21, 00:00, 3 Years ago
Sat, Sep 19, 20, 00:00, 4 Years ago
;