Javascript – Pass script tag value to input tag in html

html, javascript

I am trying to pass a particular variable value from the script tag to an input tag. But somehow it is not working.

I am trying to pass variable1 value from the below code from script tag to input tag.

So suppose variable1 value is John then this line in my code will look like this-

<input ONCLICK="window.location.href='some_url&textId=John'">

Below is the code

<html><head>    <title>Applying</title></head><body><script>function getUrlVars() {    // some code}var variable1 = getUrlVars()["parameter1"];var variable1 = unescape(variable1);// some more code</script><input ONCLICK="window.location.href='some_url&textId=variable1'"></body></html>

Can anyone explain me what wrong I am doing?

Best Solution

Try it that way:

var variable1 = getUrlVars()["parameter1"];variable1 = unescape(variable1);document.getElementById('Apply').onclick = function() {    window.location.href = 'some_url&textID=' + variable1;};

That attaches a function to the onclick event that exactly does what you want. For the initial input element simply remove the onclick attribute:

<input name="Apply" type="button" id="Apply" value="Apply" />