Skip to content
Advertisement

javascript equivalent of jquery all children selector

I have the following jquery selector which I am trying to convert to regular javascript.

$("#lelement>*").on("dblclick", function(){
});

what would the equivalent be with regular javascript ? Can I do this ?

document.getElementById('element').childNodes.addEventListener("dblclick", function(e){
});

Advertisement

Answer

One elegant, non-jQuery way to do this would be via querySelectorAll():

    document.querySelectorAll('#lelement > *').forEach(function(node) {
        node.addEventListener("dblclick", function(e){
           console.log('double clicked on child of #element');
        });
    })
p {
margin:1rem;
background:red;
color:white;
}
<div id="lelement">
<p>Double click me - Child 1</p>
<p>Double click me - Child 2</p>
</div>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement