tags:

views:

27

answers:

4

update all the href value in a page using jquery. I have the href="http://www.google.com" coming through out the page and i wanted to update the href mentioning above to be changed to "http://www.test.com" how i can get this done.

+1  A: 
$('[href]').each(function () {
  $(this).attr('href', 'http://www.test.com');
});
Codler
+1  A: 
$('a[href*="google"]').attr('href', 'http://www.test.com');

The selector will go through all links that have google somewhere in their href attribute with *= and if so, it will update their attribute accordingly.

Sarfraz
This is what i wanted.
Patrick
A: 

Save this as an .html file for a complete working example!

<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt;
  <script>
    $(document).ready(function(){
        $("a[href*='http://www.google.com']").attr('href','http://www.test.com').html('Test.com');
    });
  </script>
</head>
<body>
     <a href="http://www.google.com"&gt;Google&lt;/a&gt;
     <a href="http://www.google.com"&gt;Google&lt;/a&gt;
     <a href="http://www.google.com"&gt;Google&lt;/a&gt;
     <a href="http://www.NotGoogle.com"&gt;Not Google</a>
</body>
</html>
Brandon Boone
A: 

use selectors

<script type="text/javascript">
  $("a[href*='http://www.google.com']").attr('href','http://www.test.com');
</script>
clumsyfingers