Stack Guru
Stack Guru

Reputation: 871

How to handle click event in javascript?

Today is my first day at a new job (a Front End Lead on a large website.) One of my tasks is to implement a button that fires an event when it's clicked. I had no clue, so after googling a bit, I came up with this:

<html>
<head>
<script type="text/javascript">
function popup(); 
{
    alert("Hello World") ==> alert("Hello World");
}
</script>
</head>
<body>

<input type="button" value="Click Me!" onclick="popup()"></input><br></br>

</html>    
</body>

The problem is, when I click my button, nothing happens.

EDIT Updated based on MICHEL's comments

Upvotes: 1

Views: 10704

Answers (5)

Maxim Dsouza
Maxim Dsouza

Reputation: 24

function should not have a semi colon after its declaration ... A simple tutorial on javascript would give you a start on javascript...

Upvotes: 0

Michel
Michel

Reputation: 264

Remove semi-colon after

Bad :

function popup(); 

Good :

function popup()

You might want to add one after alert("Hello World") ==> alert("Hello World");

Upvotes: 1

Scherbius.com
Scherbius.com

Reputation: 3414

This should work

<html>
<head>
</head>

<body>

<script type="text/javascript">
function popup() 
{
    alert("Hello World");
}
</script>

<input type="button" value="Click Me!" onclick="popup()"><br/><br/>

</body>
</html>

Upvotes: 2

Naftali
Naftali

Reputation: 146310

remove the semi-colon:

function popup()
{
    alert("Hello World")
}

that should work ^_^

see it working here: http://jsfiddle.net/maniator/fNeJh/

Upvotes: 6

Matt Ball
Matt Ball

Reputation: 359836

Your HTML is invalid. Change

</html>    
</body>

to

</body>
</html>

Also, the <br> tag has no content, so you don't need a closing tag at all (for HTML) or you can write it as a self-closing tag: <br/>.

As far as the JavaScript error goes, Neal's answer is correct.

Upvotes: 3

Related Questions