mtwallet
mtwallet

Reputation: 5096

jQuery get href attr remove .html

I have the following jQuery:

$('.aboutUs a').live('click', function(e){
    e.preventDefault();

    var clickedElement = $(this).attr('href');
});

If I console.log the result I will get page.html. What I would like to is remove the .html but not sure how. Can anyone help? Many thanks.

Upvotes: 2

Views: 924

Answers (2)

a'r
a'r

Reputation: 37009

You can use a regex to remove the .html from the end of the string, eg.

"page.html".replace(/.html$/,'')

Upvotes: 3

James Allardice
James Allardice

Reputation: 166021

You could just replace the .html with nothing:

var clickedElement = $(this).attr("href").replace(".html", "");

Note that if your string could potentially contain other instances of ".html" then the regex answer is better, because in that case, this will remove the first occurence rather than the last. But if that's not the case (and it sounds quite unlikely) then this should work fine.

Upvotes: 6

Related Questions