codek codek - 6 months ago 15
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

li
and its attribute
data-filter
(with a dot behind).

For example, if the page has this url:

www.myexample.com/#commisions


the
li
would look like this:

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


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

Try this

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