dyk dyk - 7 months ago 32
Javascript Question

JavaScript cannot get firstChild

I am trying to get first child of my div "tick_list" to insert new element but it returns undefined.

$(function() {
var tick_list = $('#tick_list');

socket.on('tick', function(data){
console.log(tick_list.firstElementChild);
});
});


enter image description here

Answer

tick_list is a jQuery object which does not have firstElementChild property.

You could use tick_list.children().eq(0) to get the first child (still a jQuery object).

There are many ways to get the first child, and here is a jsperf test for them: