Amin
Amin

Reputation: 434

How to use jquery to pick url and then change it to another one?

Here is my html code:

 <a href="http://www.google.com/" target="_blank">Google</a> 

I need a jquery code to find Google.com and change it to Yahoo.com.

For example, if you click on the Google text then yahoo.com will open in your browser.

Is it possible to use jquery to find all Google texts in current page and replace them with Yahoo?

Upvotes: 1

Views: 233

Answers (3)

user1403947
user1403947

Reputation:

Code Example:

$('a[href*="google.com"]').each( function (i) {
  $(this).attr("href","http://yahoo.com");
});

Fiddle: http://jsfiddle.net/f4WmR/


Useful links

JQuery Selectors

JQuery .attr() Documentation

Upvotes: 0

boxmein
boxmein

Reputation: 875

This should do the trick:

$('a[href*="google"]').each(function() {
  $(this).attr('href', $(this).attr('href').replace(/google/i, 'yahoo');
});

Aside from that I see no reason sinister enough to actually use this...

Upvotes: 2

Jeffpowrs
Jeffpowrs

Reputation: 4540

This is a strange question:

$('a[href*="google"]').each( function (i) {
  $(this).attr("src","http://yahoo.com")
});

Upvotes: 0

Related Questions