codek codek - 1 year ago 44
jQuery Question

Add text and attribute to div depending on url with jquery

On page load, I'm trying to add the text (capitalized) of the last part of the url to a

and its attribute
(with a dot behind).

For example, if the page has this url:

would look like this:

<li><a data-filter=".commisions" href="#">Commisions</li>

I have tried with this method but it doesn't remove the
from the text:

var url = document.URL
var cuturl = url.substr(url.lastIndexOf('/') + 1);
$( ".navbar-brand" ).html( cuturl );

Any idea how can achieve it?

Answer Source

Try this

var url = document.URL;
var cuturl = url.split("#")[1];
$("li a").html(cuturl);
$("li a").attr('data-filter','.'+cuturl);