JavaScript Resources

Single and Double Quotes in Event Handlers

The use of quotes inside of an event handler can be a bit tricky for beginners to master.

The JavaScript code that you want to execute is placed to the right of the equals sign, and must always be enclosed within a set of double quotes " ". The double quotes, in this instance, have a special meaning; they are used to identify the statements to execute to the web browser.

As a result, double quotes cannot be used within any statement contained inside of an event handler. Instead, you must use single quotes ' ' in places where you would ordinarily use double quotes within a statement.

Take a look again at the previous event handler example:

    <img src="smiley.gif" onClick="alert('Hey!')" />

This is why the word 'Hey!' is surrounded by single quotes above instead of double quotes.

Next up ... case sensitivity in event handlers