Note that there are some explanatory texts on larger screens.

plurals
  1. POusing location.pathname to add a class
    primarykey
    data
    text
    <p>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 <code>&lt;a</code>> tag that contains the html "Airports". </p> <p>This is what I have so far:</p> <p><strong>JQuery</strong></p> <pre><code>path_name=location.pathname; if ((path_name.search("airports") &gt; 0) { $("div.more li a").find(":contains('airports')").addClass("current-menu-item"); } </code></pre> <p><strong>The html looks like this:</strong></p> <pre><code>&lt;ul&gt; &lt;a class="static" href="#"&gt;ALL&lt;/a&gt; &lt;li&gt;&lt;a class="static" href="#"&gt;AIRPORTS&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p><strong>URL is this:</strong></p> <pre><code>http://localhost/site/?type=airports </code></pre> <p>Is this a syntax problem or an "Try another way" problem?</p> <p>UPDATE:</p> <p>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);</p> <p>gave me the pop-up</p> <pre><code>?type=airports </code></pre> <p>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.</p> <p><strong>UPDATE 2</strong></p> <p>This is working for an isolated incident. </p> <pre><code>var search_name = location.search; if (search_name.search("airports") &gt; 0) { alert(search_name); $("div.more a:contains('AIRPORTS')").addClass("active"); } }); </code></pre> <p>I would like to make a bit more flexible by passing a variable.</p> <p>How do you do thins? This is what I have so far</p> <pre><code>$(function() { var search_name = location.search; if (search_name.search("+ search_name +") &gt; 0) { alert(search_name); $("div.more a:contains('AIRPORTS')").addClass("active"); } }); </code></pre> <p>But this is not working.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload