user3013365 user3013365 - 5 months ago 66
HTML Question

JavaScript Not able to set focus to first li element within ul

I have below code:

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

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];

My second attempt is like this :

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

But none of the above works

Any ideas?


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

<li tabindex="-1">...</li>

Try this fiddle: jsfiddle