最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling a JavaScript function when a button is clicked in HTML - Stack Overflow

programmeradmin1浏览0评论

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:

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./YPGdbTVQ

Share Improve this question edited Aug 15, 2012 at 4:01 Alex Kalicki 1,5339 silver badges20 bronze badges asked Aug 15, 2012 at 3:51 StevenSteven 4951 gold badge6 silver badges8 bronze badges 1
  • 1 I'm sensing a disturbance in your copy-paste. Try again? – Winfield Trail Commented Aug 15, 2012 at 3:52
Add a ment  | 

2 Answers 2

Reset to default 6

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! ;)

you cant put double quotes inside double quotes, so make your code like this

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

评论列表(0)

  1. 暂无评论