Mike Vierwind
Mike Vierwind

Reputation: 1520

Remove the # from the variable

I have this script:

<a href="#extensive" class="forward next-content">Menu item</a>

I can not edit the html. Only the js. This is my js:

// Click
$('.next-content').click(function() {
    var url = $(this).attr('href');

    console.log(url);
});

When i click on the a button. I get the variable. The href in the variable. The variable is #extensive. But, how can i remove the # from the variable?

Thanks for help

Upvotes: 0

Views: 77

Answers (5)

bashleigh
bashleigh

Reputation: 9324

I'm unsure really. This may help. I would have said replace would have been your best option:

url.replace('#', '');

Upvotes: 0

Maxim Pechenin
Maxim Pechenin

Reputation: 344

You can google it easily...

var url = $(this).attr('href').substr(1);

Upvotes: 0

Gabber
Gabber

Reputation: 5462

Can't you just do

// Click
$('.next-content').click(function() {
    var url = $(this).attr('href');
    url=url.replace("#","");
    console.log(url);
});

Upvotes: 0

Declan Cook
Declan Cook

Reputation: 6126

You can replace the '#' character

url.replace('#','')

or substring

url.substring(1)

Upvotes: 1

Samuel Caillerie
Samuel Caillerie

Reputation: 8275

var url = $(this).attr('href').substring(1);

Upvotes: 6

Related Questions