JavaScript Events


Traditional DOM event handlers

  • Use onclick() to send an event when the element is clicked.
  • Use onfocus() to send an event when an input is selected.
  • Use onblur() to send an event when an input loses focus.
  • Use onmouseover() to send an event when the mouse moves over an element.
  • Use onmouseleave() to send an event when the mouse moves off of an element.

Change the element class to change it's look

  • Use elem.classList.add( "class1", "class2", "class3" ); to add classes to the HTML element.

Validate forms

    
    function validateForm( form ) {
    if(form.elements[1].value == "1") {
    
    alert( "Input must have a value" );
    form.elements[1].focus();
    return false;
    }
    return true;
    }