5

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

Jamie

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I've got a bit of an issue in that i am adding the variable onchange of a drop down list and it keeps adding it on to href everytime change the drop down?
if you don`t require a valid html you can add attribute "basic_href" to the A-tag and change the code of Adam like: $('#link').attr('href', $('#link').attr('basic_href') + myvariable);
0

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

Comments

0

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;

}

Comments

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.