简体   繁体   中英

How do I get a number inside a div element to decrement based on an non matching number entered in text field?

I wanted to first say that I am new as new can get to javascript. I am trying to do a remake of the number guessing game where I have a div with a number in it that I want decreased when I guess wrong. I have been able to get the game to print out a message saying whether I guessed right or wrong, but I can't get it to decrement the number in the other div box I created. I tried to use parseInt, but I feel that I'm so new that I am way off. I had already pre-assigned a number inside the div, but I'm assuming that it is in string form and must somehow be converted before I can even decrease it.

I have a simple text field(for entering the guessed number attempt), a message field above it in a div, a div below that shows the number I guessed after entered, and a div with guesses left. I gave up and deleted the code, any help would be appreciated. I just want to know how to decrease that guesses left number from 10 to below when I've guessed wrong.

When you submit your guess number in your form, you'll need to update the "guesses remaining" value. Just have your HTML display the value.toString() and update the value in JS using a click handler on your form ( https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick ). This should get you moving in the right direction.

That form submission should also validate your input answer btw.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM