- JavaScript HTML DOM Events
- Example
- Click on this text!
- Example
- Click on this text!
- HTML Event Attributes
- Example
- Assign Events Using the HTML DOM
- Example
- The onload and onunload Events
- Example
- The onchange Event
- Example
- The onmouseover and onmouseout Events
- The onmousedown, onmouseup and onclick Events
- More Examples
- HTML DOM Event Object Reference
- Handling load events in a content document
- JavaScript Event Handlers :Loading/ Unloading a Document
- onBlur and onFocus Event Handlers
- onBlur and onBlur Event Handlers
- Running a Script from the onLoad Event Handler
- Using addBehavior() and removeBehavior()
- addBehavior() and removeBehavior() Method Lab
- Using Event Bubbling with the onClick Event
- Using Event Bubbling
- Using the fireEvent() Method
- fireEvent() Method
- HTML DOM Body Object
- Example
- Create a Body Object
- Body Object Properties
- Standard Properties and Events
- Related Pages
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
JavaScript HTML DOM Events
A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element.
To execute code when a user clicks on an element, add JavaScript code to an HTML event attribute:
- When a user clicks the mouse
- When a web page has loaded
- When an image has been loaded
- When the mouse moves over an element
- When an input field is changed
- When an HTML form is submitted
- When a user strokes a key
In this example, the content of the element is changed when a user clicks on it:
Example
Click on this text!
In this example, a function is called from the event handler:
Example
Click on this text! HTML Event Attributes
To assign events to HTML elements you can use event attributes.
Example
Assign an onclick event to a button element:
In the example above, a function named displayDate will be executed when the button is clicked.
Assign Events Using the HTML DOM
The HTML DOM allows you to assign events to HTML elements using JavaScript:
Example
Assign an onclick event to a button element:
In the example above, a function named displayDate is assigned to an HTML element with the id=»myBtn» .
The function will be executed when the button is clicked.
The onload and onunload Events
The onload and onunload events are triggered when the user enters or leaves the page.
The onload event can be used to check the visitor’s browser type and browser version, and load the proper version of the web page based on the information.
The onload and onunload events can be used to deal with cookies.
Example
The onchange Event
The onchange event is often used in combination with validation of input fields.
Below is an example of how to use the onchange. The upperCase() function will be called when a user changes the content of an input field.
Example
The onmouseover and onmouseout Events
The onmouseover and onmouseout events can be used to trigger a function when the user mouses over, or out of, an HTML element:
The onmousedown, onmouseup and onclick Events
The onmousedown , onmouseup , and onclick events are all parts of a mouse-click. First when a mouse-button is clicked, the onmousedown event is triggered, then, when the mouse-button is released, the onmouseup event is triggered, finally, when the mouse-click is completed, the onclick event is triggered.
More Examples
onmousedown and onmouseup
Change an image when a user holds down the mouse button.
onload
Display an alert box when the page has finished loading.
onfocus
Change the background-color of an input field when it gets focus.
Mouse Events
Change the color of an element when the cursor moves over it.
HTML DOM Event Object Reference
For a list of all HTML DOM events, look at our complete HTML DOM Event Object Reference.
Handling load events in a content document
JavaScript Event Handlers :Loading/ Unloading a Document
onBlur and onFocus Event Handlers
/* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
var alert("Element \"" + id + "\" has blurred.")
var alert("Element \"" + id + "\" has received focus.")
onBlur and onBlur Event Handlers
Lorem ipsum dolor sit amet, consectetaur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim adminim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Bis nostrud exercitation ullam mmodo consequet. Duis aute involuptate velit esse cillum dolore eu fugiat nulla pariatur. At vver eos et accusam dignissum qui blandit est praesent luptatum delenit aigueexcepteur sint occae.
Unte af phen neigepheings atoot Prexs eis phat eit sakem eit vory gast te Plok peish ba useing phen roxas. Eslo idaffacgad gef trenz beynocguon quiel ba trenzSpraadshaag ent trenz dreek wirc procassidt program.
Running a Script from the onLoad Event Handler
Using addBehavior() and removeBehavior()
/* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
myPBehaviorID = document.all.myP.addBehavior("makeHot.htc") setInitialColor()
if (document.all.myP.readyState == «complete») < var select = document.forms[0].colorChoice var color = select.options[select.selectedIndex].value document.all.myP.setHotColor(color) >else
document.all.myP.removeBehavior(myPBehaviorID)
> function setColor(select, color)
if (document.all.myP.hotColor) < document.all.myP.setHotColor(color) >else
var num = document.all.myP.behaviorUrns.length var msg = "The myP element has " + num + " behavior(s). " if (num > 0) < msg += "Name(s): \r\n" for (var i = 0; i < num; i++) < msg += document.all.myP.behaviorUrns[i] + "\r\n" >> alert(msg)
addBehavior() and removeBehavior() Method Lab
This is a sample paragraph. After turning on the behavior, it will turn your selected color when you mouse down anywhere in this paragraph.
Using Event Bubbling with the onClick Event
Using Event Bubbling
Using the fireEvent() Method
/* JavaScript Bible, Fourth Edition by Danny Goodman John Wiley & Sons CopyRight 2001
// assemble a couple event object properties function getEventProps()
var msg = "" var elem = event.srcElement msg += "event.srcElement.tagName: " + elem.tagName + "\n" msg += "event.srcElement.id: " + elem.id + "\n" msg += "event button: " + event.button return msg
> // onClick event handlers for body, myP, and mySPAN function bodyClick()
var msg = "Click event processed in BODY\n\n" msg += getEventProps() alert(msg) checkCancelBubble()
var msg = "Click event processed in P\n\n" msg += getEventProps() alert(msg) checkCancelBubble()
var msg = "Click event processed in SPAN\n\n" msg += getEventProps() alert(msg) checkCancelBubble()
> // cancel event bubbling if check box is checked function checkCancelBubble()
event.cancelBubble = document.controls.bubbleOn.checked
> // assign onClick event handlers to three elements function init()
document.body.onclick = bodyClick document.all.myP.onclick = pClick document.all.mySPAN.onclick = spanClick
> // invoke fireEvent() on object whose ID is passed as parameter function doFire(objID)
var newEvt = document.createEventObject() newEvt.button = 3 document.all(objID).fireEvent("onclick", newEvt) // don"t let button clicks bubble event.cancelBubble = true
fireEvent() Method
This is a paragraph (with a nested SPAN) that receives click events.
HTML DOM Body Object
You can access a element by using getElementsByTagName():
Example
Tip: You can also access a element by using the document.body property.
Create a Body Object
You can create a element by using the document.createElement() method:
Body Object Properties
Property | Description |
---|---|
aLink | Not supported in HTML5. See CSS :active Selector instead. Sets or returns the color of an active link in a document |
background | Not supported in HTML5. Use style.backgroundImage instead. Sets or returns the background image for a document |
bgColor | Not supported in HTML5. Use style.backgroundColor instead. Sets or returns the background color of a document |
link | Not supported in HTML5. See CSS :link Selector instead. Sets or returns the color of unvisited links in a document |
text | Not supported in HTML5. Use style.color instead. Sets or returns the color of the text in a document |
vLink | Not supported in HTML5. See CSS :visited Selector instead. Sets or returns the color of visited links in a document |
Standard Properties and Events
The Body object also supports the standard properties and events.
Related Pages
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.