0

I'm trying to run a JS method with a custom text variable as a parameter. I need to be able to write some text in a form, and then send that value to the method to execute it. I'm not sure why it's not working - it seems to be receiving the value of the VALUE as "" or blank. How would I go about doing this?

    <FORM NAME="myform" ACTION="" METHOD="GET">
Choose a Place: <INPUT TYPE="text" NAME="inputbox" VALUE="" id = "place"><P>
</FORM>

<button type="button" onclick="buttonGenerator()">Generate Postcard</button>

<script>
    var x = document.getElementById('place').value;
    function buttonGenerator(){
        generate(x);
    }
</script>

1 Answer 1

2

Your x variable is being set when the script first runs (when the page is loading). You want to avoid setting it until the button is clicked. Simply move it into the function and you should be set:

function buttonGenerator(){
    var x = document.getElementById('place').value;
    generate(x);
}
Sign up to request clarification or add additional context in comments.

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.