0

I'm trying to make a simple mad libs type game. To do so I need to do the following:

  1. Have space that user can input text
  2. Retrieve text that user inputted
  3. Assign text to variables using Javascript
  4. Place variables in Mad libs

I know how to do this using something like:

var userAnswer = prompt("Give me an answer");

However, I want to get the input from a text input field. I was trying to do the following but I got stuck:

  1. Have input area with id="input1"
  2. Create function that takes content of #input1 and assign to a variable.
  3. Use button to run the function

I will then later use these variables in my story

<label for='input1'>Verb + ing</label><input id='input1'>

<script>
    var setInputs = function() {
        var space1 = document.getElementById("input1").innerHTML;
    }
</script>

<button onclick="setInputs">Click me</button>

I'm I going about this the correct way?

Fiddle: http://jsfiddle.net/6rjf5k9n/

1
  • document.getElementById("input1").value Commented Sep 5, 2014 at 4:22

1 Answer 1

1

Try This

<label for='input1'>Verb + ing</label><input id='input1'>

        <script>
        var setInputs = function() {
            var space1 = document.getElementById("input1").value;
            alert(space1);
        }


        </script>
             <button onclick="setInputs()">Click me</button>

.value gives you the currently-set value of a form element (input, select, textarea), whereas .innerHTML builds an HTML string based on the DOM nodes the element contains.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I just had to use ".value" and then I just used document.getElementById("answers").innerHTML = space1; afterwards. However, when I call "space1" in my JS console I get "space1 is not defined." It works for me, I was just wondering why it shows as not defined in the console.

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.