1

What I am trying to do is target all the a tags within #menu-wrap li's. I'm fairly new to JS so I'm sorry if I'm missing something obvious!

JavaScript:

var menuLink = document.querySelector( '#menu-wrap li' );    
for (var i = 0; i < menuLink.children.length; i++) {
var childElement = menuLink.children[i];
childElement.addEventListener('click', doSomething, false);
}

function doSomething() {
    alert("Hello");
}

HTML

<div class="menu-wrap" id="menu-wrap">
    <nav class="menu">
        <ul id="menu-mobile-menu" class="menu">
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            <li><a href="#">Link 1</a></li>
        </ul>
    </nav>              
</div>
2
  • 1
    a better way: you can use one event on the <UL> instead of many events on the <LI>s. it's called delegation, look into it before you learn the harder-old-fashioned way of explicit manual subscription. Commented Jan 10, 2016 at 21:30
  • @dandavis, do you have a good reference about event delegation in raw javascript? I have already used the jQuery.on and interested to know the other way. Thanks in advance Commented Jan 10, 2016 at 22:37

2 Answers 2

3

querySelector:

Returns the first element within the document (using depth-first pre-order traversal of the document's nodes|by first element in document markup and iterating through sequential nodes by order of amount of child nodes) that matches the specified group of selectors.

You want to use querySelectorAll and then loop over the resulting node list (or you want to bind your event handler to #menu-wrap itself and then use event.target to determine which list item was clicked on).

List items are not designed to be interactive controls. You should use a link or button instead.

Sign up to request clarification or add additional context in comments.

3 Comments

+ one, also for the last advice because robots semantically consider <li> to be list items and not links.
More to the point, users of linear navigation devices (i.e. those who don't use a mouse / trackpad / etc, such as many screen reader users), can't focus the elements in order to activate them.
yes true! thank you. could you just please intervene on Rando Hinn answer (someone downvoted it without explaining while it is working for me here on jsfiddle). What are reasons of it nor being the appropriate approach? Thanks again for your usual sharing
-1

Document.querySelector gets the first element only. But you can do this with classes. Do this. Just attatch class sth to anything you want to have the function.

HTML

<div class="menu-wrap" id="menu-wrap">
            <nav class="menu">
                <ul id="menu-mobile-menu" class="menu">
                    <li class="sth"><a href="#">Link 1</a></li>
                    <li class="sth"><a href="#">Link 2</a></li>
                    <li class="sth"><a href="#">Link 1</a></li>
                </ul>
            </nav>
</div>

JS

var menuLink = document.getElementsByClassName("sth");
for(var i = 0; i <  menuLink.length; i++ ) {
    var childElement = menuLink[i];
    childElement.addEventListener('click', doSomething, false);
}

function doSomething() {
    alert("Hello");
}

2 Comments

This annswer has been downvoted without explanation from downvoter, while I need as a newbie to know what is the reason behind choosing querySelectorAll insteal of class attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.