1

This code replaces only parts of a string for the first link, but how do I modify this code to take advantage of the click and replace parts of the string for the other links as well, the other links will have different text e.g for "ca-test" it could be anything you want I just want to get this working thanks!

$("#box1").click(function(){
var url = $('.mylink').attr('href')
url = url.replace('us-test', 'replaced-text')
$('.mylink').attr('href', url) 
 });  

<a class="mylink" href="http://google.com/en/get.php?chrome=us-test">Test</a> 
<a class="mylink2" href="http://google.com/en/get.php?chrome=ca-test">Test</a>  
<a class="mylink3" href="http://google.com/en/get.php?chrome=tk-test">Test</a>
4
  • How do you know what text to replace and what to replace it with for each link? That information needs to be stored somewhere, e.g., in a data- attribute in the html for each link or somewhere in your JS code. (Also, I think you should be using the id attribute rather than the class attribute since your class names seem like unique identifiers.) Commented Feb 18, 2012 at 5:24
  • do you have many links or just three Commented Feb 18, 2012 at 5:29
  • "How do you know what text to replace and what to replace it with for each link" is stored on the jquery code "us-test" changes to "replaced-text" as for the rest of your comment i'm ok with what i'm doing Commented Feb 18, 2012 at 5:32
  • @COLDTOLD i have 5 links Commented Feb 18, 2012 at 5:33

1 Answer 1

1

. it not the best solution since it not clear how your page function but maybe it useful for you

 $("#box1").click(function(){
    var rep1=new Array(".mylink",".mylink2",".mylink3");
    var rep2=new Array("us-test","ca-test","tk-test");
    var rep3=new Array("us-test1new","ca-test1new","tk-test1new");
    replace(rep1, rep2, rep3);
     });  


    function replace(a, b, c)

    {
    for(var i=0; i < a.length;  i++)
    {
    var url = $(a[i]).attr('href');
    url = url.replace(b[i], c[i]);
    $(a[i]).attr('href', url);
     }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

It works perfectly now thanks COLD TOLD... and thank you for your time i really appreciated. :)

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.