1

I have this simple Html code here.

<form action="" method="post">
    Text Box:<input type="text" name="host" value="default value" size=30 onchange="updateTextBox()" />
    <input type="submit" name="submit" value="update value in text box to current value"/>
</form>

Every time I click the button, I want the default value of the textbox to be updated to the value I most recently entered.

1
  • What have you done so far? Commented Jun 6, 2014 at 19:22

1 Answer 1

1
<script>
    /* give a name to the form and write it where "form_name" is */
    var newText = document.forms["form_name"]["host"].value;

    /* add this chunk in the submit button (onclick = "changeText()") */
    function changeText()
    {
        /* give an id to the text box input tag and replace "input_id" with that */
        document.getElementById("input_id").value = newText; 
    }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

can you make me an example ? it doesnt seem to work... <html> <script> var newText = document.forms["mainform"]["input"].value; function changeText() { document.getElementById("input1").value = newText; } </script> <form name="mainform" action="" method="post"> <input type="text" name="input" id="input1" /> <input type="submit" onclick ="changeText()" name="Submit" value="Submit!" /> </form> <html>
<input type="submit" onclick ="changeText(event)" name="Submit" value="Submit!" /> changeText(e) { event.preventDefault(); //functionality here $('form').submit(); }

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.