Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This function cancels an event and prevents it from bubbling up.

Parameters:

  • event – reference to the event name to cancel

Example:

In this example, we prevent the onclick event if other fields on the screen are not validated properly.

Note: We assume that validated() is a custom-built function that is available for use. We also assume that validated() returns true or false to indicate the validity of certain fields on the screen.The following can be used on the onkeydown event to restrict which keys can be pressed. 

Code Block
javascript
javascript
function gradeKeys(event, element) {
  var submitButtonkey = getObj("my_button");

submitButton.onclick = function(event)

{
     if(!validated())      //if fields on the screen are not valid
     {
          preventEvent(*event*);
     }event.keyCode;
  switch (key) {
    case 65: // allow A
    case 66: // allow B
    case 67: // allow C
    case 68: // allow D
    case 70: // allow F
    case 8:  // allow backspace
    case 46: // allow delete
    case 37: // allow left arrow
    case 39: // allow right arrow
    case 40: // allow down arrow
    case 38: // allow up arrow
    case 13: // allow enter
      break;
    default: // disallow all other keys
      preventEvent(event);
  } 
}