Yannick
Yannick

Reputation: 3653

Replace value from text in href attribute

I'm trying to change the value of expand to 0 when the DOM is ready.

I've tried different things

$('.link-comment').attr('href').find("expand=1").replaceWith('expand=0');

But it's not working. So I can I change the value of expand to 0

<a class="link-comment" href="/eventcomments/create-like/227?expand=1">

Upvotes: 0

Views: 82

Answers (3)

user2399923
user2399923

Reputation: 126

Try this:

$('.link-comment').attr('href',$('.link-comment').attr('href').replace("expand=1", "expand=0"));

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$('.link-comment').filter('[href*="expand=1]"').attr('href', function(idx, href){
    return href.replace('expand=1', 'expand=0')
})

Upvotes: 0

PSL
PSL

Reputation: 123739

Try it this way:

$(function(){
    $('.link-comment').attr('href', function (_, cur) {
        return cur.replace(/expand=1/, "expand=0");
    });
});

fiddle

Upvotes: 1

Related Questions