Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript function to add class to a list element based on # in url
    primarykey
    data
    text
    <p>I am trying to create a javascript function to add and remove a class to a list element based on the #tag at the end of the url on a page. The page has several different states, each with a different # in the url.</p> <p>I am currently using this script to change the style of a given element based on the # in the url when the user first loads the page, however if the user navigates to a different section of the page the style added on the page load stays, I would like it to change.</p> <pre><code>&lt;script type="text/javascript"&gt; var hash=location.hash.substring(1); if (hash == 'strategy'){ document.getElementById('strategy_link').style.backgroundPosition ="-50px"; } if (hash == 'branding'){ document.getElementById('branding_link').style.backgroundPosition ="-50px"; } if (hash == 'marketing'){ document.getElementById('marketing_link').style.backgroundPosition ="-50px"; } if (hash == 'media'){ document.getElementById('media_link').style.backgroundPosition ="-50px"; } if (hash == 'management'){ document.getElementById('mangement_link').style.backgroundPosition ="-50px"; } if (hash == ''){ document.getElementById('shop1').style.display ="block"; } &lt;/script&gt; </code></pre> <p>Additionally, I am using a function to change the class of the element onClick, but when a user comes to a specific # on the page directly from another page and then clicks to a different location, two elements appear active.</p> <pre><code>&lt;script type="text/javascript"&gt; function selectInList(obj) { $("#circularMenu").children("li").removeClass("highlight"); $(obj).addClass("highlight"); } &lt;/script&gt; </code></pre> <p>You can see this here: <a href="http://www.perksconsulting.com/dev/capabilities.php#branding" rel="nofollow noreferrer">http://www.perksconsulting.com/dev/capabilities.php#branding</a></p> <p>.</p> <p>Sorry for the lack of clarity. The question is how to modify the function listed first to add and remove the class: highlight, from the elements based on the hash in the url rather than onclick as it currently does. I realize that I am modifying the style of the element inline with my current function, I would modify the class rather than the style.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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