{"id":2814,"date":"2015-02-04T16:33:00","date_gmt":"2015-02-05T00:33:00","guid":{"rendered":"http:\/\/eloine.com\/blog\/?p=2814"},"modified":"2015-02-04T17:23:21","modified_gmt":"2015-02-05T01:23:21","slug":"highlight-link-to-current-page-with-javascript","status":"publish","type":"post","link":"https:\/\/eloine.com\/blog\/archives\/2814","title":{"rendered":"Highlight link to current page with javascript"},"content":{"rendered":"<p>If you have a menu on your website and you would like to provide some visual feedback with that one of the links in the menu bar is the page you are on it&#8217;s pretty easy with some HTML and CSS.<\/p>\n<p>All you have to do is add a class name to the HTML for that link and then style that class name with CSS.<\/p>\n<p>For example just style the class of &#8220;current&#8221;:<\/p>\n<pre><code><span class=\"tag\">&lt;li<\/span> <span class=\"atn\">class<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"<span style=\"color: #ff0000;\">current<\/span>\"<\/span><span class=\"tag\">&gt;&lt;a<\/span> <span class=\"atn\">href<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"http:\/\/www.somepage.com\"<\/span><span class=\"tag\">&gt;<\/span><span class=\"pln\">some page<\/span><span class=\"tag\">&lt;\/a&gt;&lt;\/li&gt;<\/span> <\/code><\/pre>\n<p>Unfortunately if you are using a common navigation system where you have one file that has all the navigation elements linked to hundreds of pages it&#8217;s not possible to change the link style for every link. It defeats the purpose of a common shared file.<\/p>\n<p>Thankfully I found an <a href=\"http:\/\/snook.ca\/archives\/javascript\/clear_links_to_1\">old blog post by Jonathan Snook<\/a> that had a script that worked perfectly to strip the link from the current page button. See how &#8220;Drama Club&#8221; in this screen shot is now light gray and slightly out-dented? It is now an inactive link so if you hover your mouse over it the cursor will not change.<\/p>\n<p><a href=\"http:\/\/eloine.com\/blog\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-04-at-4.11.16-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2815\" src=\"http:\/\/eloine.com\/blog\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-04-at-4.11.16-PM.png\" alt=\"Screen Shot 2015-02-04 at 4.11.16 PM\" width=\"491\" height=\"641\" srcset=\"https:\/\/eloine.com\/blog\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-04-at-4.11.16-PM.png 491w, https:\/\/eloine.com\/blog\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-04-at-4.11.16-PM-230x300.png 230w, https:\/\/eloine.com\/blog\/wp-content\/uploads\/2015\/02\/Screen-Shot-2015-02-04-at-4.11.16-PM-153x200.png 153w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/a><\/p>\n<p>All you need to make this work is a link to your JS file in your HTML document in the head section like this<\/p>\n<p id=\"line1\">&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">type<\/span>=&#8221;<a class=\"attribute-value\">text\/javascript<\/a>&#8221; <span class=\"attribute-name\">src<\/span>=&#8221;<a class=\"attribute-value\" href=\"view-source:http:\/\/snook.ca\/technical\/currentlink\/clearlink.js\">clearlink.js<\/a>&#8220;&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;<\/p>\n<p>Then in the JS file this is the below content is all you need. After that the rest is styling with CSS!<\/p>\n<pre>\/*\r\nCLCP v2.1 Clear Links to Current Page\r\nJonathan Snook\r\nThis code is offered unto the public domain\r\nhttp:\/\/www.snook.ca\/jonathan\/\r\n*\/\r\n\r\nwindow.onload = clearCurrentLink;\r\n\r\nfunction clearCurrentLink(){\r\n    var a = document.getElementsByTagName(\"A\");\r\n    for(var i=0;i&lt;a.length;i++)\r\n        if(a[i].href == window.location.href.split(\"#\")[0])\r\n            removeNode(a[i]);\r\n}\r\n\r\nfunction removeNode(n){\r\n    if(n.hasChildNodes())\r\n        for(var i=0;i&lt;n.childNodes.length;i++)\r\n            n.parentNode.insertBefore(n.childNodes[i].cloneNode(true),n);\r\n    n.parentNode.removeChild(n);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you have a menu on your website and you would like to provide some visual feedback with that one of the links in the menu bar is the page you are on it&#8217;s pretty easy with some HTML and CSS. All you have to do is add a class name to the HTML for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,8],"tags":[],"class_list":["post-2814","post","type-post","status-publish","format-standard","hentry","category-graphic-design","category-web-design"],"_links":{"self":[{"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/posts\/2814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/comments?post=2814"}],"version-history":[{"count":3,"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/posts\/2814\/revisions"}],"predecessor-version":[{"id":2818,"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/posts\/2814\/revisions\/2818"}],"wp:attachment":[{"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/media?parent=2814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/categories?post=2814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eloine.com\/blog\/wp-json\/wp\/v2\/tags?post=2814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}