Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a JavaScript function when a button is clicked in HTML

I wrote a simple JavaScript code that allows people to enter a number and it should return either FizzBuzz, Fizz, Buzz or just the number if it did not meet the conditions of the FizzBuzz game. I then tried to create a button in HTML with the following code:

<button onclick="fBCalc(prompt("Enter a number!"))">FizzBuzz</button>

I have also tried this:

<input type="button" name="FizzBuzz" value="FizzBuzz" onClick="fBCalc(prompt("Enter a number!"))" />

Both methods did not work. I have searched Google for an answer but I have tried them and nothing happens when I click the button. I made sure that the function was called correctly. This works without the button so I do not know why it shouldn't work with a button. Unless a button is unable to prompt o.o. Does anyone know what is wrong?

Here is a pastebin to my full HTML code: http://pastebin.com/YPGdbTVQ

like image 597
Steven Avatar asked Aug 15 '12 03:08

Steven


1 Answers

You haven't pasted your code properly here, but I did spy what I suspect is your issue on your Pastebin:

<button onclick="fBCalc(prompt("Enter a number!"))">FizzBuzz</button>

You can't put quotes inside of quotes the way you have here, because the browser can't figure out which ones do what. Try swapping the inner quotes with single-quotes:

 <button onclick="fBCalc(prompt('Enter a number!'))">FizzBuzz</button>

Coincidentally, good luck with your grades! ;)

like image 109
Winfield Trail Avatar answered Sep 30 '22 01:09

Winfield Trail