0

Admittedly, javascript is not my strong suit. I need to be able to append a parameter from the url into three a href links using javascript. The structure of the url will be www.mysite.com?parameter.

Currently, the links are:

https://www.acme.com/sui?bc=QOS-B20 https://www.acme.com/sui?bc=USE-BFN https://www.acme.com/sui?bc=USP-BFN

The resulting links should be:

https://www.acme.com/sui?bc=QOS-B20&sc=parameter https://www.acme.com/sui?bc=USE-BFN&sc=parameter https://www.acme.com/sui?bc=USP-BFN&sc=parameter

I already have jquery loaded on the page.

I tried to follow the directions on this post: Passing an URL parameter to a href link using Javascript but it didn't work.

Thanks!

1
  • Are you going to ask a question or just tell us what you're working on is currently not working? Commented Mar 9, 2015 at 21:54

1 Answer 1

0

You can do it easily with jquery

$('.class-of-links-to-update').each(function(){
  $(this).attr('href',$(this).attr('href')+'&'+param));
});

.class-of-links-to-update is value inside class attribute on href

<a class="class-of-links-to-update" ....

I used each function to loop through results returned by jQuery.

this represents current element in list of results and $(this) will convert element into jQuery object so we can use jQuery methods to get and set attributes of HTML element.

So just execute above JavaScript to update your results.

e.g.

function updateHrefs(param) {
    $('.class-of-links-to-update').each(function(){
      $(this).attr('href',$(this).attr('href')+'&'+param));
    });
}

end then call the function when ever you want

e.g. on button click

$('#id-of-button').click(function(){
  updateHrefs('sc=parameter');
});
Sign up to request clarification or add additional context in comments.

2 Comments

Cool, this is very helpful. Follow-up question. Where do I place the two code snippets? Thank you!
everything is explained, if you want you can put it on button click or on load I explained above

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.