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

  • SEARCH
  • Home
  • 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 7713551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:54:12+00:00 2026-06-01T01:54:12+00:00

I want to set the menu to active when click on it. This is

  • 0

I want to set the menu to active when click on it. This is what I’ve tried :

<script language="javascript" type="text/javascript">
  var select;
  $(document).ready(function () {
    $.getJSON(url, function (data) {
     $.each(data, function (index, dataOption) {

      var new_li = $("<li class='level1' id='select_list'><a href='javascript:void(0);' id='" + dataOption.ID + "' class ='selectedcategory'>"  + dataOption.Name + "</a>");

      $('a#' + dataOption.ID).click(function () {
        select = "selected";
        $('.level1').attr("id",select);
      });
     });
    });
  });
</script>

What I tried to do is to set the id of ‘level1’ to selected, when I click on that link. But my coding is set all the link to selected, even I click only 1 link.

Could anyone give me the solution please.

Thanks so much.

  • 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-01T01:54:13+00:00Added an answer on June 1, 2026 at 1:54 am

    That happens because you are calling .attr() on all .level1 element instead of only the one being clicked by its child so,

    change

    $('.level1').attr("id",select);
    

    to

    $(this).parent().attr('id', select);
    

    You also need to remove the “selected” id from the other <li>‘s by

    $('.level1').removeAttr('id');
    

    So the complete code looks like:

      $('a#' + dataOption.ID).click(function () {
          $('.level1').removeAttr('id');
          $(this).parent().attr("id", 'selected');
      });
    

    One suggestion though, class is usually used in situation like this when you want to have a selected type of a list of menu, if you are using class you’d do something like

    $('a#' + dataOption.ID).click(function () {
        $('.level1').removeClass('selected');
        $(this).parent().addClass('selected');
    }
    

    Looks more concise that way, but I’ll leave it up to you 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set an active class for a CSS image menu. I tried
For example, I have node/31. And I want drupal to set menu active item
I want to set a menu option icon for a drawable that is in
How to set the max height of a css drop-down menu without using javascript?
Hello i have the following xml for menu i want to set the background
To be Google friendly, I want to change this script a little bit. I
hello everyone i want to set the quit menu in my app . The
I want to set another class for the clicked menu-part. Clicking generates url with
i want to set the CommandTarget property of menu item to the TextControl which
I want to set select and link in one line, but select is upper.

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.