Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
  body {
    font-family: sans-serif;
  }
  p {
    margin: 0px;
  }
</style>
</head>
<body>
  <div id='list_container'>
    <ul id='list_1'>
      <li>List 1 item 1</li>
      <li>List 1 item 2</li>
    </ul>
    <ul id='list_2'>
      <li>List 2 item 1</li>
      <li>List 2 item 2</li>
    </ul>
  </div>
 
window.onload = function() {
  
  // Hook up the contextmenu event on the container, not
  // on each list:
  hookEvent(document.getElementById('list_container'),
            'contextmenu',
            handleListContextMenu);
  
  // Our handler function
  function handleListContextMenu(event) {
    var target;
    
    // Handle IE-vs-the-world difference
    event = event || window.event;
    
    // Find out what the actual target element clicked was
    target = event.target || event.srcElement;
    
    // See if it or an ancestor of it is one of our lists
    while (target &&
           (target.tagName !== "UL" || !target.id || target.id.substring(0, 5) !== "list_")) {
      target = target.parentNode;
    }
    
    // Did we find a list?
    if (target) {
      // Yes, handle this.
      if (event.preventDefault) {
        event.preventDefault();
      }
      display("List '" + target.id + "' context menu");
      return false;
    }
  }
  
  function hookEvent(element, event, handler) {
    if (element.addEventListener) {
      element.addEventListener( event, handler, false);
    }
    else if (element.attachEvent) {
      element.attachEvent('on' + event, handler);
    }
    else {
      element['on' + event] = handler;
    }
  }
  
  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }
};
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers