Reputation: 449
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>
Upvotes: 0
Views: 4127
Reputation: 24312
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>
Upvotes: 5
Reputation: 540
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>
Upvotes: 0
Reputation: 181
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");
}
Upvotes: 0
Reputation: 94
<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
Upvotes: 0
Reputation: 846
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"); }
Upvotes: 1