Add parameter at the end of the link with jQuery

1

I'm trying to get some specific URL's on a page, and add a parameter at the end of the link, but I'm not getting success as the object is returned instead of the link string + parameter.

I made a very simple example of the code:

var links = jQuery('a[href*="google.com.br"]');
links.attr('href', links + 'parametros_adicionais');

Running this code returns me the URL with this parameter at the end [object% 20Object] test

Can you help me?

    
asked by anonymous 23.02.2017 / 16:01

2 answers

1

The correct would look like this:

var links = jQuery('a[href*="google.com.br"]');
links.attr('href', links.attr('href') + 'parametros_adicionais');

Remembering that the code with Jquery must always be inside a "ready":

    $(document).ready(function(){
       var links = jQuery('a[href*="google.com.br"]');
       links.attr('href', links.attr('href') + 'parametros_adicionais');
    })
    
23.02.2017 / 16:19
1

Links is an object that contains all the elements, you must go through a[href*="google.com.br"] and change one at a time.

var param = 'SoUmParametro';
var src;
var links = jQuery('a[href*="google.com.br"]');
links.each(function() {
   src = $(this).prop('href');
   $(this).prop('href', src+param);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="http://google.com.br">link1</a>
<a href="http://google.com.br">link2</a>
    
23.02.2017 / 16:13