One event handler, many actions
The secret to having an event handler be able to call multiple functions/statements inside of it is the semicolon (;). Simply add your statements or functions inside the event handler, but separate EACH of them with a semicolon. Let's continue on with our CodingForums example to create a button that first thanks your surfer before sending his way to Dynamic Drive:
<form> <input type="button" value="Click here!" onClick="alert('You are about to visit a help forum'); window.location='http://www.dynamicdrive.com/forums/'"> </form>
As you can see, I simply separated the
alert()
statement and the location statement using a semicolon, and viola,
two JavaScript statements being executed by a single event handler. There is
no limit on how many functions/statements you can have in one event handler-
simply separate each of them using a semicolon.
- Tutorial introduction
- One event handler, one action
- One event handler, many actions
- JavaScript cut-and-pasters, listen up!