admin管理员组

文章数量:1425674

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?

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?

Share Improve this question edited Jul 27, 2013 at 21:33 asked Jul 27, 2013 at 21:26 user2442489user2442489 4
  • 1 It's unclear what you are trying to acplish... – Jivings Commented Jul 27, 2013 at 21:31
  • I am trying to pass variable1 value to input tag at the end. – user2442489 Commented Jul 27, 2013 at 21:32
  • You want it to appear in the input field when it is clicked? – Jivings Commented Jul 27, 2013 at 21:33
  • at onclick call a function, inside that function set window.locatio.href ! – user2587132 Commented Jul 27, 2013 at 21:35
Add a ment  | 

6 Answers 6

Reset to default 1

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" />

If you wish to perform inline functions, you need to wrap the code in an executable closure:

<input name="Apply" type="button" id="Apply" value="Apply" ONCLICK="(function() {window.location.href='your_data'})();">

As this can be largely unmaintainable, I remend you abstract this functionality into a more organized place in your application.

(function(window, $, undefined) {
  // assuming you use jQuery
  $('#Apply').click(function() {
   window.location.href = '';// your code
  })
})(window, $);

I may be totally misunderstanding what you want to do, but I hope this helps.

The whole url parameters bit is surely unnecessary.

You can just set the value attribute in the field:

var field = document.getElementById('textfield');
var value = 'Some text';

field.addEventListener("click", function () {
    this.setAttribute('value', value);
});

Here's a jsfiddle example: http://jsfiddle/LMpb2/

You have it inside the ' ' you need to add it into the string. So try

         "window.location.href='some_url&textId='+variable1+';'"

I would change it to the following if your trying to bind the click handler to this input element:

<html>
<head>
    <title>Applying</title>
</head>
<body>

<script>
function getUrlVars() {

    // some code

}
var variable1 = getUrlVars()["parameter1"];
var variable1 = unescape(variable1);

document.getElementById("Apply").onclick = function() {
    window.location.href='some_url&textId=' + variable1;
}

// some more code

</script>

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

</body>
</html>

I haven't tested it yet but it should work.

at onclick call a function, inside that function set window.locatio.href !

a sample

<script>
var url="www.google.";
function myfunc(){
alert(url);
}
</script>

<input type="button" onclick="myfunc()" value="btn" >

http://jsfiddle/CgKHN/

本文标签: javascriptPass script tag value to input tag in htmlStack Overflow