?
Solved

Trying to addClass to a menu item based off the title of the page using jquery.

Posted on 2016-07-29
3
Medium Priority
?
71 Views
Last Modified: 2016-07-29
Hi,
hope this makes some sense....
I have a menu and the items are set dynamically using a loop and I bring in the sidebar containing the menu with an include.
So each of the <li> in the menu have the id of the menu name which is also the page title.
I'm trying to set the class to 'active' for whichever menu item we are on.

For example
The menu item corresponding to my "Used Inventory" page is:
<li id="Used Inventory">Used Inventory</li> (which is also the page title tag)

So below I'm trying to figure out how to set a class of 'active' to that <li> based off the title tag.
Obviously I'm failing :(
Any idea how to get this done?

<script>
    	$('#' + $(document).attr('title')).addClass('active');
</script>

Open in new window

0
Comment
Question by:tjyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 2000 total points
ID: 41735359
Without seeing the full page structure, I'd be willing to guess, that your script is at the top of the page?
Nothing wrong with that, but in that case the rest of the page hasn't actually loaded yet when you call your script, so the target element isn't in the dom.

Wrap the function above in $(document).ready() and that might fix.

<script>
	$(document).ready(function(){
	    	$('#' + $(document).attr('title')).addClass('active');
	});
</script>

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 41735361
Thanks. No difference. I'm guessing my reference to the id tag is probably wrong?
I was guessing how to format that.
<script>
$( document ).ready(function() {
    	$('#' + $(document).attr('title')).addClass('active');
    	});
</script>

Open in new window

Could it be it doesn't have any quotes around the #Used Inventory instead of '#Used Inventory'
That being the case, not sure how to get those extra 2 quotes in there
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41735367
Its late... I must be getting sleepy.
That was the fix.
Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question