Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:09:23+00:00 2026-06-11T15:09:23+00:00

This is such a basic question but I am having trouble finding the answer

  • 0

This is such a basic question but I am having trouble finding the answer – How do I implement the action taken when a list item in an Html menu is clicked?

I’m using an list in my code as a menu, say:

<ul  >
<li id="link1"><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a>
  <ul class="level2">
    <li><a href="#">Link 2a</a></li>
    <li><a href="#">Link 2b</a></li>
    <li><a href="#">Link 2c</a></li>
  </ul></li>
</ul>

I tried giving the menu an id, id=”myMenu”, and an onclick event, and the js was called, but I couldn’t see a way to identify which item was clicked, just that an line item was clicked in myMenu.

<ul id="myMenu" onclick="gothere(id)">
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a>
  <ul class="level2">
    <li><a href="#">Link 2a</a></li>
    <li><a href="#">Link 2b</a></li>
    <li><a href="#">Link 2c</a></li>
  </ul></li>
</ul>

<script type="text/javascript">
function gothere(id)
{
alert("got here "+id) ; 
}
</script>  

I tried adding an id on a child element, id=”link1″, and that worked, but the js was called for the child and then for the parent.

<ul id="myMenu" onclick="gothere(id)">
<li id="link1" onclick="gothere(id)"><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a>
  <ul class="level2">
    <li><a href="#">Link 2a</a></li>
    <li><a href="#">Link 2b</a></li>
    <li><a href="#">Link 2c</a></li>
  </ul></li>
</ul>

I can use this approach to get what I need but is there a better way?

I am looking for something which sends the least amount of html over the wire (the reason I am redoing this menu from it’s former implementation).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:09:24+00:00Added an answer on June 11, 2026 at 3:09 pm

    You shouldn’t attach the events inline. Attach them with javascript.

    <ul id="myMenu">
    <li id="link1"><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a>
      <ul class="level2">
        <li><a href="#">Link 2a</a></li>
        <li><a href="#">Link 2b</a></li>
        <li><a href="#">Link 2c</a></li>
      </ul></li>
    </ul>​
    

    Note that I’m using jQuery for simplicity here:

    $("#myMenu").click(function(e){
        console.log(e.target);
    });​
    

    http://jsfiddle.net/2rjgd/

    With vanilla javascript the concept is the same, use the target property of the event object to find out where the click event originated from.

    http://jsfiddle.net/2rjgd/1/ shows how this would be implemented with vanilla js. (in non IE browsers)

    document.getElementById("myMenu").addEventListener("click", function(e){
        console.log(e.target);
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry this is such a basic question, but I can't find a straight answer
This may be a basic question but the answer has alluded me so far.
Sorry for such a basic question but I can't find this elsewhere How could
This may be a basic question, but I could not find an answer for
This is such a basic question in awk . But I am facing issues
This is such a basic question, but I have to ask. In SL, I
This is such a simple problem, but I can't find an answer anywhere... I
sorry this is such a simple question but I can't figure it out. How
I know this may be a very basic question, sorry if it is but
Sorry for asking such a basic question but so far all the examples that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.