0

I have attempted to produce Javascript Alert functions for my website so that they would pop up,however for some unidentified reason-they do not work.I have tested my code with W3C Validator,which resulted in 3 warnings,one of which said I had missed a body tag,yet I do not see any error within my code.Please assist?

<!DOCTYPE html>
<html>
<body>

<p>Click the button.</p>

<button onclick="myFunction()">Example 1 Answer</button>
<br>
<button onclick="2">Example 2 Answer</button><br>
<button onclick="3">Example 3 Answer</button><br>
<button onclick="4">Example 4 Answer</button>

<script>
function myFunction() {
  alert("10/63");
}
function 2() {
  alert("13/10");
}
function 3() {
  alert("37/30");
}function 4() {
  alert("4/21");
}
</script>

</body>
</html>
3
  • upvote for this little boy with big dreams Commented Aug 9, 2014 at 10:08
  • It's appreciated-thanks. Commented Aug 9, 2014 at 10:10
  • Hmmm,...still not working-as always,only the first fraction,10/63 is working. Commented Aug 9, 2014 at 10:17

5 Answers 5

5

You cannot have just a number as a function name. Change it like following:

function myFunction() {
  alert("10/63");
}
function a2() {
  alert("13/10");
}
function a3() {
  alert("37/30");
}
function a4() {
  alert("4/21");
}

And the HTML should change like this:

<p>Click the button.</p>
<button onclick="myFunction()">Example 1 Answer</button>
<br>
<button onclick="a2()">Example 2 Answer</button>
<br>
<button onclick="a3()">Example 3 Answer</button>
<br>
<button onclick="a4()">Example 4 Answer</button>

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

It does work,but then again-only the first button works-10/63.The rest are static.
Yup-I did,however someone already answered my question.Thanks,though!
1

Function names can't begin with digits. If you copy paste your following function in the console you will get unexpected number reference.

function 2() { alert("4/10"); }

Comments

0

Use for old browsers:

       <script language="text/javascript"> 

Function names can not start with numbers, fix that also.

Edit:

For HTML4

       <script type="text/javascript"> 

For HTML5

       <script>

Comments

0

http://jsfiddle.net/c1pcc4xv/

 <form action="">
     <input type="button" id="myButton" value="Add"/>
</form> 
//script
var myBtn = document.getElementById('myButton');
myBtn.addEventListener('click', function(event) {
alert("yo");
});

make your code simple, and avoid function names starting with number....Hope it helps

1 Comment

Perhaps,perhaps.however,I need multiple buttons.
0

Here is a JS Fiddle

Yes, you can not use just numbers as a function names. And also, wrap it in the head tag, define your function before calling it in your html.

function myFunction() {
   alert("10/63");
}
function myFunction2() {
   alert("13/10");
}
function myFunction3() {
   alert("37/30");
}
function myFunction4() {
  alert("4/21");
}

2 Comments

Thank You-It's appreciated.
You're welcome. When you open my jsfiddle, please note that on the left panel, I've selected "No wrap -in <head>" option.

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.