By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

I need help hiding/ disabling buttons.

Blue1244
P: 39
Hello, I'm 13 trying my luck with js. I am trying to make an in browser game. It is a simple math game, just basic stuff.I am trying to have a list of buttons on the webpage, but you need to go down in order without being able to pick question two instead of question one. How do i make the other buttons inactive until completion of the first question.

Here is my code:(Not really good)
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. </head>
  4. <script language="javascript">
  5. alert("Are you sure you want to play this game?");
  6. var Name = prompt("What is your name?");
  7. </script>
  8.  
  9. <script type="text/javascript">
  10. document.write(Name);
  11. </script>
  12.  
  13. <center><button id="button1" onClick="start()">First Question</button></center>
  14.  
  15. <script>
  16. function start() {
  17. var one = prompt("What is 3 * 4 ?");
  18.  
  19. if (one!=12)
  20.   {
  21.    {
  22.  alert("Correct");
  23.   }
  24.  else 
  25.  {
  26.  alert("Incorrect");
  27.  }
  28. }
  29. </script>
  30. </html>
  31.  
Aug 21 '13 #1

✓ answered by Dormilich

until post #11, the if/else would throw a syntax error.

Share this Question
Share on Google+
23 Replies


Dormilich
Expert Mod 5K+
P: 8,639
It is a simple math game
3 * 4 = 7 ???

some notes:
  • missing Doctype
  • missing <title>
  • the language attribute is deprecated, use type="text/javascript"
  • <center> is deprecated, use CSS instead
  • several typos
  • document.getElementById() is a function, not a primitive for assignment
  • if/else: check the brace count
Aug 22 '13 #2

Blue1244
P: 39
yeah that's just some mixed up code I was doing this at 4:40 AM before school started and some stuff I was working on and decided not to use it and forgot to remove some stuff.and i only gave you the important stuff because I didn't want to give every thing because that would have made more lines. I was planning on removing the
Expand|Select|Wrap|Line Numbers
  1. document.getElementById()
. Thank you for the help on my code. Oh and the
Expand|Select|Wrap|Line Numbers
  1. document.getElementById()
was when i was trying to figure out how to delete a button, witch is what i asked this question for in the first place.
Aug 22 '13 #3

Dormilich
Expert Mod 5K+
P: 8,639
was when i was trying to figure out how to delete a button
simple enough:
Expand|Select|Wrap|Line Numbers
  1. // or whatever you like to reference the button
  2. var button = document.getElementById("theButton");
  3.  
  4. button.parentNode.removeChild(button);
Aug 22 '13 #4

Blue1244
P: 39
Thank you now I am going to input this so i can try to get this working. After I get this working it should be a smooth road the rest of the way. Unless I wanted to add a score and scoreboard but that will be later.
Aug 22 '13 #5

Blue1244
P: 39
Wait is there a way of deleting the question 2 buttons and above. on entering the page, and enabling them after question one is finished? Sorry if i sound like i need help on everything.
Aug 22 '13 #6

Blue1244
P: 39
I'm going to try to keep the code UTD as much as i can on here.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. </head>
  5. <script type="text/javascript">
  6. var w= confirm("Are you sure you want to play this game?");
  7.  
  8.     if (w==true)
  9.     {
  10.     var button = document.getElementById("theButton");
  11.  
  12.     button.parentNode.removeChild(button);
  13.     }
  14.     else
  15.     {
  16.     window.close();
  17.     }
  18. </script>
  19. <script type="text/javascript">
  20. var Name = prompt("What is your name?");
  21. </script>
  22.  
  23. <script type="text/javascript">
  24. document.write(Name);
  25. </script>
  26.  
  27. <center><button id="button1" onClick="start()">First Question</button></center>
  28.  
  29. <script type="text/javascript">
  30. function start() {
  31. var one = prompt("What is 3 * 4 ?");
  32.  
  33. if (one!=12)
  34.   {
  35.    {
  36.  alert("Correct");
  37.   }
  38.  else 
  39.  {
  40.  alert("Incorrect");
  41.  }
  42. }
  43. </script>
  44. </html>
  45.  
Aug 22 '13 #7

Dormilich
Expert Mod 5K+
P: 8,639
I would disable the buttons rather than removing ...

anyways, your if/else is still wrong.
Expand|Select|Wrap|Line Numbers
  1. // with appropriate indenting
  2. if (...)
  3. {
  4.     {
  5.         alert(...);
  6.     }
  7.     else
  8.     {
  9.         alert(...);
  10.     }
  11. }
I’m pretty sure that throws an unexpected "else" error.
Aug 22 '13 #8

Blue1244
P: 39
Okay I will try to fix this as soon as possible, I go to school in about 1 hour. No, I'm not sleep deprived I have a really weird sleep schedule.
Aug 22 '13 #9

Blue1244
P: 39
And the title of this question is "I need help removing/disabling buttons" not to be rude but you were the one who brought up removing so I thought that would work. Sorry if I sound rude.
Aug 22 '13 #10

Blue1244
P: 39
Is this better?
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE hmtl>
  2. <html>
  3. <head>
  4. </head>
  5. <script>
  6. function start() 
  7. {
  8. var one = prompt("What is 3 * 7 ?"); 
  9.  
  10. if (one!=12)
  11.   {
  12.  
  13.     alert("Correct");
  14.  
  15.   }
  16. else
  17. {
  18.  
  19.     alert("Incorrect");
  20.  
  21. }
  22.  
  23. </script>
  24. <script type="text/JavaScript">
  25. var Name = prompt("What is your name?");
  26. </script>
  27.  
  28. <script type="text/JavaScript">
  29. document.write(Name);
  30. </script>
  31.  
  32. <center><button id="button1" onClick="start()">First Question</button></center>
  33. <center><button id="button2" onClick="two()">Second Question</button></center>
  34.  
  35. </html>
Aug 22 '13 #11

Dormilich
Expert Mod 5K+
P: 8,639
in the end, it’s you who has to choose, which of the many options you want to follow. and each coder has his own taste.
Aug 22 '13 #12

Dormilich
Expert Mod 5K+
P: 8,639
Is this better?
you should check the error console (hit F12 to bring it up).

other than that, lines 10-14 are a bit superfluous. if you decline, the window is closed anyways, if not, why removing the button when you can omit it in the first place?
Aug 22 '13 #13

Blue1244
P: 39
Before i head off to school i am going to have to post this, don't have time to ask a whole other question. With this code I cant ask the prompt and have the if/else in the same function, how do i fix this?
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE hmtl>
  2. <html>
  3. <head>
  4. </head>
  5. <script>
  6. function start()
  7. {
  8. var one = prompt("What is 3 * 4 ?");
  9. }
  10. </script>
  11. <script> 
  12.  
  13. if (one!=12)
  14.   {
  15.  
  16.     alert("Correct");
  17.  
  18.   }
  19. else
  20. {
  21.  
  22.     alert("Incorrect");
  23.  
  24. }
  25.  
  26. </script>
  27. <script type="text/JavaScript">
  28. var Name = prompt("What is your name?");
  29. </script>
  30.  
  31. <script type="text/JavaScript">
  32. document.write(Name);
  33. </script>
  34.  
  35. <center><button id="button1" onClick="start()">First Question</button></center>
  36. <center><button id="button2" onClick="two()">Second Question</button></center>
  37.  
  38. </html>
  39.  
Aug 22 '13 #14

Blue1244
P: 39
No I want it to close window, as in the whole tab not just the alert/dialog/prompt box.
Aug 22 '13 #15

Dormilich
Expert Mod 5K+
P: 8,639
With this code I cant ask the prompt and have the if/else in the same function, how do i fix this?
put the if/else inside the function.
Aug 22 '13 #16

Dormilich
Expert Mod 5K+
P: 8,639
No I want it to close window
which "it"?
Aug 22 '13 #17

Blue1244
P: 39
Never mind, but originally I did have the if/else within the function but the function wouldn't carry out the prompt.
Aug 22 '13 #18

Dormilich
Expert Mod 5K+
P: 8,639
until post #11, the if/else would throw a syntax error.
Aug 22 '13 #19

Blue1244
P: 39
Okay i figured out why the function wouldn't run the prompt, I had thrown in a unwanted brace.
Aug 22 '13 #20

Blue1244
P: 39
Thank you for your help.
Aug 22 '13 #21

Blue1244
P: 39
Oh and one more question how do i run one function from within another function?
Aug 22 '13 #22

Blue1244
P: 39
Never Mind i don't need an answer.
Aug 22 '13 #23

Dormilich
Expert Mod 5K+
P: 8,639
Expand|Select|Wrap|Line Numbers
  1. function test1()
  2. {
  3.   // ...
  4. }
  5.  
  6. function test2()
  7. {
  8.   // ...
  9.   test1();
  10.   // ...
  11. }
Aug 22 '13 #24

Post your reply

Sign in to post your reply or Sign up for a free account.