ok pretty simple but i dont know how...
i just want to make an active state (probably just make it bold)
my menu is ul-li
i cant figure out how to write it so if the url matches with one of the links, make the link bold
please help
thanks for your time
The current URL in jQuery can be obtained by using the 'href' property of the Location object which contains information about the current URL. The 'href' property returns a string with the full URL of the current page.
To Get the ID from URL with JavaScript, we can call the JavaScript string's split method on the URL string. Then we get the last part of it with the JavaScript array at method. We call split with '/' to split the url string by the / . Then we call at with -1 to get the element from the strs array.
jQuery attribute equals selectors If we want to force a link to a given URL to open in a new tab, we would use the following: $(document). ready(function(){ $('a[href=http://www.google.com]').click(function(){ window. open(this.
By comparing the hostname of the string URL to the hostname of the web page URL, you can determine whether the link is external or not.
Here's a short way to select links like that:
$('ul > li a[href$="' + window.location.pathname + '"]').css('font-weight','bold');
Or perhaps better like this, which does an exact match of both pathname
attributes:
$('ul > li a[href]').filter(function() {
return this.href.pathname === window.location.pathname;
}).css('font-weight','bold');
If you're using the full domain in the href
, you could change it to:
return this.href === window.location;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With