Event_Handling in javascript
Event_Handling in javascript
events.
HTML Events
An HTML event can be something the browser does, or something a user does.
HTML elements.
<button> element:
<html>
<body>
<p id="demo"></p>
</body>
</html>
In the example above, the JavaScript code changes the content of the element
with id="demo".
In the next example, the code changes the content of its own element (using
this.innerHTML):
<!DOCTYPE html>
<html>
<body>
</body>
</html>
JavaScript code is often several lines long. It is more common to see event
<html>
<body>
<script>
function displayDate() {
document.getElementById("demo").innerHTML = Date();
</script>
<p id="demo"></p>
</body>
</html>
onmouseout The user moves the mouse away from an HTML element
browser actions:
Many different methods can be used to let JavaScript work with events:
● You can assign your own event handler functions to HTML elements
● You can prevent events from being sent or being handled
DOM Events allow JavaScript to add event listener or event handlers to HTML
elements.
button.addEventListener("click", myFunction);
nd
eration
art
d unloaded
changed
u context menu
target
target
dragover A dragged element is over the drop DragEvent
target
target
nge
listening")
ange mode
part of a URL
specified media
event source
element
mouseenter The pointer is moved onto an element MouseEvent
element
l instead
opened
paused
or buffered
changed
context menu
stalled The browser is trying to get unavailable Event
media data
media data
media)
element
screen
d
unload A page has unloaded UiEvent, Event
ge (includes muting)
an element
ith the HTML DOM, JavaScript can access and change all the elements of an
HTML document.
the page.
HTML:
interface that allows programs and scripts to dynamically access and update the
The HTML DOM is a standard object model and programming interface for
HTML. It defines:
In other words: The HTML DOM is a standard for how to get, change, add, or
A property is a value that you can get or set (like changing the content of an
HTML element).
Example
The following example changes the content (the innerHTML) of the <p> element
with id="demo":
<html>
<body>
<p id="demo"></p>
<script>
</script>
</body>
</html>
he innerHTML Property
The easiest way to get the content of an element is by using the innerHTML
property.
The innerHTML property is useful for getting or replacing the content of HTML
elements.
The innerHTML property can be used to get or change any HTML element, including
If you want to access any element in an HTML page, you always start with
Method Description
me)
ame)
Property Description
content element
HTML element
element.style.property = new style Change the style of an HTML
element
Method Description
HTML element
Method Description
Method Description
document.getElementById(id).onclick Adding event handler code to an
HTML DOM methods are actions you can perform (on HTML Elements).
HTML DOM properties are values (of HTML Elements) that you can set or
change.
languages).
A property is a value that you can get or set (like changing the content of an
HTML element).
Example
The following example changes the content (the innerHTML) of the <p> element
with id="demo":
<html>
<body>
<p id="demo"></p>
<script>
</script>
</body>
</html>
property.
element.
In the example above the getElementById method used id="demo" to find the
element.
property.
The innerHTML property is useful for getting or replacing the content of HTML
elements.
innerHTML property.
This example changes the value of the src attribute of an <img> element:
<!DOCTYPE html>
<html>
<body>
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>
</body>
</html>
document.write()