I want to loop through an array in order to add CSS to menu links with jQuery. If a certain string appears in the URL, a certain CSS is assigned to a menu link that contains the same string.
Here's HTML (not sure if it really helps, but here it is):
<ul>
<li>
<a href="http://mysite.com/">HOME</a>
<a href="http://mysite.com/about">ABOUT</a>
<a href="http://mysite.com/brands">BRANDS</a>
<a href="http://mysite.com/investors">INVESTORS</a>
<a href="http://mysite.com/news">NEWS</a>
<a href="http://mysite.com/videos">VIDEOS</a>
<a href="http://mysite.com/contact">CONTACT</a>
</li>
</ul>
Here's my code snippet:
var url_link = new Array();
url_link[0] = 'about';
url_link[1] = 'the-company';
url_link[2] = 'employment';
url_link[3] = 'customer-service';
url_link[4] = 'faqs';
url_link[5] = 'brands';
url_link[6] = 'news';
url_link[7] = 'videos';
url_link[8] = 'contact';
for (var i=0; i<url_link.length; i++) {
if (location.href.indexOf(url_link[i])>=0) {
$('.appearance-menus-'+url_link[i]+'>a').css("color", "#636363");
$('.appearance-menus-'+url_link[i]+'>a').mouseout(function() {
$(this).css("color", "#636363");
});
}
}
For some reason, this snippet breaks the website, and I suspect it's the problem of concatenating an array element into the jQuery selector. I must have messed up the syntax.
What is the proper way to do that?