0

I'm new to jQuery and I'm trying to replace numerous link by another one, but I can't seem to find why it doesn't, could you help me please.

Code:

First attempt:

jQuery(document).ready(function() {

    $("a").each(function() { 

        newUrl += $(this).href+ textOfNew + " ";

        this.href = this.href.replace((this.href), newUrl);

    });

});

Second attempt:

jQuery(document).ready(function() {

    $("a").each(function() { 
        $("$(this).href").val(function(i, val) {
            var newUrl = "test" ;

            return = $(this).href.replace($(this).href, newUrl);
        }); 
    });
}); 

3 Answers 3

2

try this

   $("a").each(function() { 

          var newUrl =  $(this).attr('href')+ textOfNew + " ";

              $(this).attr('href',newUrl);

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

1 Comment

You don't need each(), just pass an anonymous function to the attr() method. As @adeneo shows.
2
jQuery(function($) {
    $("a").attr('href', function(i, href) {
        return href + '/test';
    });
});

FIDDLE

Comments

0
$("a").each(function() { 

    newUrl += $(this).attr("href") + textOfNew + " ";

    $(this).attr("href", newUrl);

});

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.