开发者

javascript add variable onto end of link

开发者 https://www.devze.com 2023-01-13 20:34 出处:网络
I\'m trying to add a variable i created on to the end of one of my links but not sure how to do it? <a href=\"../../availability/default.aspx?propid=\' + myvariable + \'\">Link</a>

I'm trying to add a variable i created on to the end of one of my links but not sure how to do it?

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

Any ideas?

Thanks

Jami开发者_开发百科e


Add an 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);


Something like this will create a closure which will store your original HREF property:

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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号