codek
codek

Reputation: 343

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?

Upvotes: 1

Views: 59

Answers (2)

Raj_King
Raj_King

Reputation: 556

Try this

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

Upvotes: 2

gurvinder372
gurvinder372

Reputation: 68413

Simply replace "\" with "#"

var cuturl = url.substring(url.lastIndexOf('#') + 1);

or

var cuturl = url.split("#")[1];

Upvotes: 1

Related Questions