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 6805399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:34:54+00:00 2026-05-26T19:34:54+00:00

I’m having problems with selectors in jQuery. Or (what I don’t think) with my

  • 0

I’m having problems with selectors in jQuery. Or (what I don’t think) with my jQuery code.

Let me say about my problem.
I have a Rails project and a I want to insert in my menu, the class “active” if the current page is same at the menu.

For example:

Current page: Posts
My menu:

  • Pages (class: none)
  • Users (class: none)
  • Posts (class: “active”)
  • Blablabla …

My HTML/Rails/jQuery code (if you’re a Rails developer, this is my layout):

Menu:

<ul id="nav">
  <li id="dashboard"><%= link_to "Dashboard", admin_index_path %></li>
  <li id="notes"><%= link_to "Notes", admin_notes_path %>
   <ul>
    <li><a href="#">List notes</a></li>
    <li><a href="#">Add note</a></li>
   </ul>
  </li>
  <li id="projects"><a href="#">Projects</a>
   <ul>
    <li><a href="#">List projects</a></li>
    <li><a href="#">Add project</a></li>
   </ul>
  </li>
  <li id="users"><a href="#">Users</a></li>
</ul>

Script:

<script>
 $("#hld #wrapper #header #nav li").addClass(function() {
   var addedClass;

   if ($(this).attr('id') === "<%= content_for(:title).downcase %>") {
    return "active";
   }

   return addedClass;
 });
</script>

<!--[if IE]><script type="text/javascript" src="/assets/excanvas.js"></script><![endif]-->  
<script type="text/javascript" src="/assets/jquery.js"></script>
<script type="text/javascript" src="/assets/jquery.img.preload.js"></script>
<script type="text/javascript" src="/assets/jquery.filestyle.mini.js"></script>
<script type="text/javascript" src="/assets/jquery.wysiwyg.js"></script>
<script type="text/javascript" src="/assets/jquery.date_input.pack.js"></script>
<script type="text/javascript" src="/assets/facebox.js"></script>
<script type="text/javascript" src="/assets/jquery.visualize.js"></script>
<script type="text/javascript" src="/assets/jquery.visualize.tooltip.js"></script>
<script type="text/javascript" src="/assets/jquery.select_skin.js"></script>
<script type="text/javascript" src="/assets/jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="/assets/ajaxupload.js"></script>
<script type="text/javascript" src="/assets/jquery.pngfix.js"></script>
<script type="text/javascript" src="/assets/custom.js"></script>

Look for the script and there’ll have my script in jQuery of course.
So, what’s the problem?

Thanks for helping, I tried to be clear.
Lucas Moreira.

  • 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-05-26T19:34:55+00:00Added an answer on May 26, 2026 at 7:34 pm

    As Jfriend00 pointed out, your selector is unnesscesarily long. '#nav li' will suffice.
    Better yet, since you are only (presumably) looking for a direct child of #nav use '#nav > li'.

    Also (someone please correct me if I’m wrong) but I think your script should go below where you load the external scripts. Trying to use JQuery before its loaded may cause problems 😉

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

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.