user2177446
user2177446

Reputation: 33

jQuery replace href and change url in every <a>

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);
        }); 
    });
}); 

Upvotes: 0

Views: 299

Answers (3)

adeneo
adeneo

Reputation: 318192

jQuery(function($) {
    $("a").attr('href', function(i, href) {
        return href + '/test';
    });
});

FIDDLE

Upvotes: 2

Lucky Soni
Lucky Soni

Reputation: 6878

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

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

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

});

Upvotes: 0

PSR
PSR

Reputation: 40318

try this

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

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

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

    });

Upvotes: 2

Related Questions