0

I'm making a game and I've a html text box, <form> <input> </form> where the users will type in a number and submit it using a html button <button>. How do I get the number using JavaScript so I can use it in my game.js so I'll know how many lives the user want when the submit button is pressed?

I'm not quite sure if this is the correct approach but I was thinking getting the id of the <input> tag and then for the JavaScript part I would use .getElementById of some sort? However, I'm not sure how to incorporate the submit button so that only when it's pressed, I will receive what the user inputted.

Or is there another better way to do this? And if possible, can you provide an example of the codes for doing this?

Let me know if any clarification is needed, thanks!

1
  • 2
    var lives = $("input").val() if you have a single input on the page, or var lives = $("#input_id").val() Commented Mar 8, 2015 at 10:03

1 Answer 1

2

There are multiple ways to do what you are asking for, one way to do it is as cristian has shown in the comment(using jquery).

In this jsfiddle page I've shown a sample way to do it just using javascript.

The code used in the page is as shown below:

document.getElementById("submit").onclick = function(){
    var a = document.getElementById("input").value;
    alert("The entered value is: "+a);
}
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.