0

Example HTML:

<p><a href='link1' target='_blank'><font color='blue' size='4'>TEXT 1</font></a></p> <br />      
<p><a href='link2' target='_blank'><font color='blue' size='4'>TEXT 2</font></a></p> <br />      
<p><a href='link3' target='_blank'><font color='blue' size='4'>TEXT 3</font></a></p> <br />

When onclick TEXT 1, TEXT 2, TEXT 3 then I want to replace link1 with linkother1, link2 with linkother2, link3 with linkother3.

How can I do that in jquery javascript? (sorry my english is very bad)

SOLUTION:

Thank's Jony-Y,

$('a[href="link1"]').attr("href", "linkother1")
$('a[href="link2"]').attr("href", "linkother2")
$('a[href="link3"]').attr("href", "linkother3")
Community
  • 1
  • 1

3 Answers3

0

Use the .attr() method to change an attribute of an element. You can use a function that receives the current value as an argument, and processes it to return the new value.

$("a").click(function() {
    $(this).attr("href", function(i, oldurl) {
        return oldurl.replace('link', 'linkother');
    });
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

of course you can my friend :)

$('a[href="link1"]').attr("href", "linkotherone")

everything is possible with jquery:)

duplicate :change hyperlin

Community
  • 1
  • 1
Jony-Y
  • 1,579
  • 1
  • 13
  • 30
  • You are absolutely right, now I can do it, thank you very much..! – Alfa Net Cell Mar 12 '15 at 00:19
  • np, I suggest that if you have a lot of links loop through them, and if you want to toggle the link and not change use the prop() jquery function on the event :) – Jony-Y Mar 12 '15 at 07:26
0

Try this code:

$(function(){
    $("a").click(function(e){
        e.preventDefault();
        $(this).attr("href", "anotherlink");
    });
});

Keep in mind that this code is for the exact HTML you provided. If you have another "a" tags on the document, you'll have to add an ID, a class or use traversing to be able to capture exactly the elements you want.

Also if you don't want to cancel the click event just remove the line "e.preventDefault();".

Diego Bauleo
  • 681
  • 3
  • 12
  • thank mr. Diego Goes Bauleo on the response, my problem is the HTML that already exist in each blog post and is not possible to change the link one by one, so I wanted to change simultaneously using jquery – Alfa Net Cell Mar 12 '15 at 00:03