Event | |
onclick |
The user clicks on an element |
oncontextmenu |
The user right-clicks on an element to open a context menu |
ondblclick |
The user double-clicks on an element |
onmousedown |
The user presses a mouse button over an element |
onmouseenter |
The pointer is moved onto an element |
onmouseleave |
The pointer is moved out of an element |
onmousemove |
The pointer is moving while it is over an element |
onmouseover |
The pointer is moved onto an element or onto one of its children |
Event | |
onkeydown |
The user is pressing a key |
onkeypress |
The user presses a key |
onkeyup |
The user releases a key |
Event | |
onerror |
An error occurs while loading an external file |
onload |
An object has loaded |
onresize |
The document view is resized |
onscroll |
An element’s scrollbar is being scrolled |
Event | |
onblur |
An element loses focus |
onchange |
The content, selection, or checked state has changed |
onfocus |
An element gets focus |
onreset |
A form is reset |
onselect |
The user selects some text |
onsubmit |
A form is submitted |
<HTMLTAG eventname = "javascript code..." >So if we use the anchor tag as an example, it will look like this:
<a href="http://www.reed.edu/" onclick="alert('hello')"> Reed College </a>
Or something more familiar
<button onclick="alert('ouch');"> click me </button>
<button id="b1"> </button> <script> var myButton = document.getElementById('b1'); function clickResponse(){ alert('you clicked me'); } myButton.onclick = clickResponse; </script>
<button id="b1"> </button>
<script>
var myButton = document.getElementById('b1');
myButton.onclick = function() {
alert('you clicked me');
}
</script>
var myButton = document.getElementById('b1'); function sayHello() { alert('hello'); } function sayBye() { alert('bye'); } myButton.addEventListener('click', sayHello); myButton.addEventListener('click', sayBye);
<html>
<head>
<script>
var b = document.getElementById('b1');
b.innerHTML = "hello"; // THIS DOES NOT WORK!!!
</script>
<head>
<body>
<button id="b1"> click me </button>
</body>
</html>
<html>
<head>
<script>
window.onload = function(){
var b = document.getElementById('b1'); // this works because this runs after the page is loaded
b.innerHTML = "hello";
}
</script>
<head>
<body>
<button id="b1"> click me </button>
</body>
</html>
<script> function where(event){ // event object contains information regarding the event alert(event.offsetX + " " + event.offsetY); // location where the click event occured } </script> <body onclick="where(event);">
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> The Title of this HTML Page </title> <script> window.onload = function(){ var myInputField = document.getElementById("myinput"); myInputField.style.width = "300px"; function myFunction(e){ var kc = e.keyCode; console.log(kc); if(kc == 27){ // Escape key alert("Possible Values for this field are... "); } if(kc == 65){ // 'a' alert("Possible Values for this field are... apple, air, auto"); } } // end of function myInputField.addEventListener('keydown', myFunction); } </script> </head> <body> This is the body of this HTML Page. <input type="text" id="myinput"> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> The Title of this HTML Page </title> <script> window.onload = function(){ var go = document.getElementById('a2'); function refuseAccess(evt){ alert("sorry, you are not allowed"); evt.preventDefault(); } go.addEventListener('click', refuseAccess); } </script> </head> <body> <a href="http://www.google.com" id="a1"> Go to Google</a> <br> <a href="http://www.google.com" id="a2"> Do not go to Google</a> </body> </html>