0

Without manually assigning an ID to each href, the goal would be to change the URLs for both hrefs to a different URL

<div class="example">
        <p><a href="http://www.google.com/example?test1332">Test Site</a></p>
        <p><a href="http://www.google.com/example?test1332">Test Site</a></p>
</div>

This is my jquery attempt which is not working

$(".example").each(function() {
    this.setAttribute("href", this.getAttribute("href").replace("http://www.test.com"));
});

This is my fiddle http://jsfiddle.net/n322j/

2 Answers 2

4

You seem to want

$('.example a').attr('href', "http://www.test.com");

If you'd want to only replace part of the URL, that is keep everything apart http://www.google.com, then you could do

$(".example a").attr('href', function(i, href) {
   return href.replace("http://www.google.com", "http://www.test.com");
});

Demonstration

Sign up to request clarification or add additional context in comments.

Comments

0

another way of doing this.

<script>
$("document").ready(function (){
$("div p a").attr('href',"http://www.google.com");
});
</script>

it also works fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.