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,259
  • 1
  • 27
  • 33
Mike Vierwind
  • 1,482
  • 4
  • 23
  • 44

5 Answers5

6
var url = $(this).attr('href').substring(1);
Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
1

You can replace the '#' character

url.replace('#','')

or substring

url.substring(1)
Declan Cook
  • 6,066
  • 2
  • 35
  • 52
0

Can't you just do

// Click
$('.next-content').click(function() {
    var url = $(this).attr('href');
    url=url.replace("#","");
    console.log(url);
});
Gabber
  • 5,152
  • 6
  • 35
  • 49
0

You can google it easily...

var url = $(this).attr('href').substr(1);
Maxim Pechenin
  • 344
  • 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
  • 8,813
  • 5
  • 29
  • 49