I would like jQuery to pull the url, then parse it to find airports. If airports is found, I would like to add a class to the list-item <a> tag that contains the html “Airports”.
This is what I have so far:
JQuery
path_name=location.pathname;
if ((path_name.search("airports") > 0) {
$("div.more li a").find(":contains('airports')").addClass("current-menu-item");
}
The html looks like this:
<ul>
<a class="static" href="#">ALL</a>
<li><a class="static" href="#">AIRPORTS</a></li>
</ul>
URL is this:
http://localhost/site/?type=airports
Is this a syntax problem or an “Try another way” problem?
UPDATE:
Using the alert suggestion, I was able to verify I am getting the url using location.search.
var search_name = location.search;
alert(search_name);
gave me the pop-up
?type=airports
So I am getting the correctly loaded variable, I just cannot write the logic to add the class. So far the suggestions are ont working.
UPDATE 2
This is working for an isolated incident.
var search_name = location.search;
if (search_name.search("airports") > 0) {
alert(search_name);
$("div.more a:contains('AIRPORTS')").addClass("active");
}
});
I would like to make a bit more flexible by passing a variable.
How do you do thins? This is what I have so far
$(function() {
var search_name = location.search;
if (search_name.search("+ search_name +") > 0) {
alert(search_name);
$("div.more a:contains('AIRPORTS')").addClass("active");
}
});
But this is not working.
…with some minor corrections, this should work: http://jsfiddle.net/a4tWE/2/
used location.href instead
the :contains selector works case sensitive
removed bracket
if ((path_name.search(“airports”) > 0) {
removed find, as contains does the job.