10

i need help on html form.

I got a javascript variable, and I am trying to pass the variable to a html form texbox. I want to display the variable on the textbox dynamically. but i do not know how to pass the variable to the html form and call the variable?

var test;

<INPUT TYPE="TEXT" NAME="lg" VALUE="" SIZE="25" MAXLENGTH="50" disabled="disabled"><BR><BR>

How do i pass test to html form and change its value?

Thanks

1
  • 10
    Don't use all-caps HTML. Commented Nov 23, 2010 at 2:19

6 Answers 6

25

Pass the variable to the form element like this

your form element

<input type="text" id="mytext">

javascript

var test = "Hello";
document.getElementById("mytext").value = test;//Now you get the js variable inside your form element
Sign up to request clarification or add additional context in comments.

5 Comments

thanks. but sorry for not being clear in my previous questions... what if the variable is from another .html file? how do i pass the variable from one html file to another html file?
@Jonathan Low You really need to provide more detail. because from what you say it sounds like you want to use a cookie or querystring to pass the variable value. But Im not entirely sure what you are trying to do. Be more specific in your question.
i got a html file in which i use javascript to get a variable. in the javascript, there is a button, upon pressing, a pop up window will open.. which will trigger another html form with a texbox inside.. i would wan to pass the javascript variable to the html form texbox and display it..
@Jonathan Low read up on Querystrings. pass the variable in the URL then get the query string value from window.location
How can you send text to non-input element, such as div?
4
<form name="input" action="some.php" method="post">
 <input type="text" name="user" id="mytext">
 <input type="submit" value="Submit">
</form>

<script>
  var w = someValue;
document.getElementById("mytext").value = w;

</script>

//php on some.php page

echo $_POST['user'];

Comments

4

instead of

document.getElementById("txtBillingGroupName").value = groupName;

You can use

$("#txtBillingGroupName").val(groupName);

instead of groupName you can pass string value like "Group1"

Comments

2

This was a problem for me, too. One reason for doing this (in my case) was that I needed to convert a client-side event (a javascript variable being modified) to a server-side variable (for that variable to be used in php). Hence populating a form with a javascript variable (eg a sessionStorage key/value) and converting it to a $_POST variable.

<form name='formName'>
<input name='inputName'>
</form>

<script>
document.formName.inputName.value=var
</script>

Comments

1

You could also use to localStorage feature of HTML5 to store your test value and then access it at any other point in your website by using the localStorage.getItem() method. To see how this works you should look at the w3schools explanation or the explanation from the Opera Developer website. Hope this helps.

Comments

-2
document.getElementById("txtBillingGroupName").value = groupName;

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.