Some Common JavaScript Events, Methods, etc.

Window Methods

Method Usage Description
prompt() var name=prompt("What is your name?");
var person = prompt("Please enter your name","Harry Potter");
prompt(text,defaultText) //defaultText is optional
Display a prompt box, asking the user to enter something.
The prompt() method returns the input value if the user clicks "OK". If the user clicks "cancel" the method returns null.
alert() alert("You have 5 minutes left!"); Display an alert box
confirm() confirm("Are you sure you want to exit?"); Displays a dialog box with a specified message, along with an OK and a Cancel button.
parseInt() var num=prompt("Enter a number");
Converts the string that's passed to it to an integer. If it can't convert it, it returns NaN.
parseFloat() var grade=prompt("Enter a grade", "99.50");
Converts the string that's passed to it to an decimal data type. If it can't convert it, it returns NaN.
setTimeout() setTimeout(function(){ alert("Hello"); }, 3000); Calls a function after a specified number of milliseconds.
clearTimeout() clearTimeout(myVar); clears a timer set with the setTimeout() method.
setInterval() setInterval(function(){ alert("Hello"); }, 3000); Calls a function at specified intervals (in milliseconds). Will continue calling the function until clearInterval() is called, or the window is closed.
clearInterval() clearInterval(myVar); clears a timer set with the setInterval() method.

DOM Properties and Methods

Property or Method Usage Description
document.write() document.write("Hello World!<br>"); Writes to the document.
getElementById() document.getElementById(id) get the element with a specified id
getElementsByTagName() document.getElementsByTagName("p") gets the elements with the specified tagname
innerHTML document.getElementByID('id').innerHTML the inner HTML of that element
focus() document.getElementById("myAnchor").focus(); Give focus to an element
getAttribute() var x = document.getElementsByTagName("H1")[0].getAttribute("class"); Returns the value of the attribute with the specified name of an element.

DOM Events

Property Usage Description
onclick() <button type="button" onclick="buttonFunction()">Click Me</button> The event occurs when the user clicks on an element.
onload() <body onload="loadFunction()">
window.onload=function(){ ... }
The event occurs when a document has been loaded.
onmouseover() <p onmouseover="mouseOverFunction()">Text Text Text</p> The event occurs when the pointer is moved onto an element.
onmouseout() <p onmouseout="mouseOutFunction()">Text Text Text</p> The event occurs when a user moves the mouse pointer out of an element

JavaScript Functions

Function Usage Description
isNaN() isNaN(x) Checks if x is an illegal number.

JavaScript Properties

Property Usage Description
prototype employee.prototype.salary=null; The prototype property allows you to add properties and methods to an object.

JavaScript Operators

Operator Usage Description
instanceof if(s1 instanceof Object) used to check the type of an object at run time.

More events go here:

For more on JavaScript Go to: