kritter
kritter

Reputation: 11

jquery replace part of a url

I know practically nothing about jquery so please help me

I try this

function googlemapLinks {
 $('div.gmnoprint a').attr("href").replace('ll','q');
}

it does not work I have a <div class="gmnoprint"> In that div Google maps puts a javascript link in maps.google.com/maps?ll=5..... I need maps.google.com/maps?q=5.....

Can you show me a function I can drop in a script file?

Upvotes: 1

Views: 9117

Answers (4)

Matt Wilkerson
Matt Wilkerson

Reputation: 1

Try this to change any link or a tag href inside the gmnoprint container. If you don't use the each loop all the hrefs will contain the first instance of the href on the page. this code looks at each a tag and replaces just that instance if it runs into 11 and changes the text to q

$('.gmnoprint a').each(function(){
    $(this).attr('href', $(this).attr('href').replace('11','q'));
});

Upvotes: 0

Dan
Dan

Reputation: 10351

Bit late to the conversation but you could try:

$('.gmnoprint a').each(function() {
    this.href = this.href.replace('ll','q');
})

The other functions seem to loop whereas this will find the link and then replace '11' with 'q'. Well, it should =P

Upvotes: 0

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

try this:

function googlemapLinks { 
    var lnk = $('div.gmnoprint a').attr("href");
    $('div.gmnoprint a').attr("href",lnk.replace('ll','q')); 
}

Upvotes: 4

David Hedlund
David Hedlund

Reputation: 129792

For setting a value with attr, you need to pass the new value as a second parameter. try this:

$('div.gmnoprint a').attr('href', $('div.gmnoprint a').attr('href').replace('ll','q'));

Upvotes: 1

Related Questions