Javascript add variable to end of link

I'm trying to add the i variable created at the end of one of my links, but not sure how to do this?

<a href="../../availability/default.aspx?propid=' + myvariable + '">Link</a> 

Any ideas?

thanks

Jamie

+4
source share
3 answers

Something like this will create a closure in which your original HREF property will be stored:

 function init() { var link = document.getElementById("link"); var hrefOrig = link.href; var dd = document.getElementById("DropDown"); dd.onchange = function(){ link.href = hrefOrig + dd.value; } } window.addEventListener("load", init, false); // for Firefox; for IE, try window.attachEvent 
0
source

Add id:

 <a id="link" href="../../availability/default.aspx?propid=">Link</a> 

JavaScript:

 document.links["link"].href += myvariable; 

JQuery

 $('#link').attr('href', $('#link').attr('href') + myvariable); 
+3
source

The solution is only to adapt the code that Adam set above:

HTML

 <a id="link" href="">Link</a> <select onchange="addVariable(this.value)">... 

Javascript

 function addVariable(myvariable){ document.links["link"].href = "../../availability/default.aspx?propid=" + myvariable; } 
0
source

All Articles