0

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

Samuel Caillerie
  • 8,111
  • 1
  • 25
  • 32
Mike Vierwind
  • 1,442
  • 4
  • 21
  • 41

5 Answers5

6
var url = $(this).attr('href').substring(1);
Samuel Caillerie
  • 8,111
  • 1
  • 25
  • 32
1

You can replace the '#' character

url.replace('#','')

or substring

url.substring(1)
Declan Cook
  • 6,009
  • 2
  • 34
  • 51
0

Can't you just do

// Click
$('.next-content').click(function() {
    var url = $(this).attr('href');
    url=url.replace("#","");
    console.log(url);
});
Gabber
  • 4,752
  • 5
  • 34
  • 47
0

You can google it easily...

var url = $(this).attr('href').substr(1);
Maxim Pechenin
  • 324
  • 1
  • 13
0

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

url.replace('#', '');
Community
  • 1
  • 1
bashleigh
  • 7,920
  • 5
  • 26
  • 45