Question:

Javascript - Update text box values

by  |  earlier

0 LIKES UnLike

So, I have a text box and I have some javascript coding prompting for a value and then I want to put that value into the aforementioned text box.

My code is:

function prompter() {

X = prompt("Enter value","")

Update() }

function Update() {

document.form.txtbox.value = x }

The syntax is correct and have confirmed it by using more coding, but the text box does not update automatically after the script is run. Am I forgetting a step? So, if anyone has any ideas, they would be great. Thanks.

 Tags:

   Report

1 ANSWERS


  1. Two problems: non-matching case x variable X not same as x

    Also if you are using name="txtbox" switch that to id="txtbox" and use

    document.getElementById("txtbox")

    so these are the correct ways (script goes in head):

    <script type="text/javascript">

    <!--

    function prompter() {

    x = prompt("Enter value","");

    Update(); }

    function Update() {

    document.getElementById ("txtbox").value = x; }

    /*** I left a space between Id and first ( so yahoo would not drop the code, should take that out ***/

    //-->

    </script>

    <body onload="prompter()">

    <form id="myform" action="">

    <p><textarea id="txtbox" rows="1" cols="20"> </textarea></p>

    </form>

    And, actually you could shorten the script up some unless you have other reasons I don't see to use two functions, you could just make it one like this:

    <script type="text/javascript">

    <!--

    function prompter() {

    x = prompt("Enter value","");

    document.getElementById ("txtbox").value = x;

    /*** I left a space between Id and first ( so yahoo would not drop the code, should take that out ***/

    }

    //-->

    </script>

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.