Event listeners are used to control the behaviour of the web page and
its interactivity with the user.
There are a few ways you can respond to user actions...
Create a button to click on - which runs a function (myFunction)
<button class="button2" onclick="myFunction(
'IDName',
'My Text Here'
)">Try Me</button>
If you want something to happen when a user, for example, clicks on the <h3> element
<h3 id="myH3" onclick="this.innerHTML='I clicked it!' ">Original text here
</h3>
Or you could even say
document.getElementById("myH3").onclick = function() {this.innerHTML="It's clicked"}
So you could ...
document.getElementById(
"myH3a").addEventListener(
"click", function() {myFunc1(this)});
The above uses an anonymous function which calls my actual NAMED function (myFunc1)
and then passes to MyFunc1 a parameter that represents this actual element (this).
In myFunc1 I will then know the actual element that I clicked on.
Remember that this. is used in javascript to represent the current item
You cannot normally pass parameters to functions with addEventListener unless you specify a
function within an anonymous function....
function() {...} is an anonymous function
So you could use an anonymous function ...
document.getElementById(
"myH3a").addEventListener(
"click", function() {...add some code...});
Or use a Named function ...
document.getElementById(
"myH3a").addEventListener(
"click", myFunction);
Note there are no () next to myFunction as you cannot pass parameters
this way so it does not like them here
If you need to pass parameters to your function see the second box (but some
say the addEventListener option is the way to go)
Click Here for a list of events
Some are listed here, there are more of course...
...eventListener event names are before the / and events to use in the element are after.
mousemove/onmousemove
mouseover/onmouseover
mouseenter/onmouseenter
mouseleave/onmouseleave
(also see 'mouseout' comments when clicking on link)
mousedown/onmousedown
mouseup/onmouseup
change/onchange
(Activates upon a change to a <select> element)
drag/ondrag
--- click here for an overview of drag/drop
dragend/ondragend
dragenter/ondragenter
dragleave/ondragleave
dragover/ondragover
dragstart/ondragstart
drop/onddop
Note...form jqEvents.html (DOM Event Listeners) use Drag/Drop so you could look there as well
keydown/onkeydown
keypress/onkeypress
keyup/onkeyup
copy/oncopy
cut/oncut
click/onclick
dblclick/ondblclick
focus/onfocus
blur/onblur (occurs when element loses focus)
Mouseover & Mouseleave
Move the mouse over the image
Above uses
onmouseover="mouseOver('emu1Over')" onmouseleave="mouseLeave('emu1Over')"
which calls my functions mouseOver and mouseLeave to make the picture appear/disappear
Below uses the eventListener to do the same thing
<object.addEventListener("mouseover", function() {
mouseOver("emu2Over", "emu2"))>
<object.addEventListener("mouseleave", function() {
mouseLeave("emu2Over"))>
The onchange/change event happens when the user selects an option from a <select> element.
eg...
<select onchange="myFunction()">
Here's a [select] box (or Drop Down Box as I know it), make a change to see what happens
Note...the onchange only activates if it changes from one value to another, different value.
Select One:
Select One:
Drag the p element ("Drag Me!") back and forth between the two rectangles:
Drag me!
Note: drag events are not supported in Internet Explorer 8 and earlier versions or Safari 5.1 and earlier versions.
The onfocus event happens when the gets focus and onblur happens when it loses focus.
eg...
<select onfocus="myFunctionGet()" onblur="myFunctionLose()">
Click in the box to set focus then click the button to lose focus on the box.
The .target event property returns the element that triggered the event.
The .target property gets the element on which the event originally occurred, opposed to the
.currentTarget property, which always refers to the element whose event listener
triggered the event.
For example(s) see...
.target event.
.currentTarget event.