0

So I recently found out that we can get the value of a bullet list through getEventTarget as shown below (credit to this OP).

HTML:

<ul id="testUL">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

JS

function getEventTarget(e) {
    e = e || window.event;
    return e.target || e.srcElement;
}

var ul = document.getElementById('testUL');
ul.onclick = function (event) {
    var target = getEventTarget(event);
    alert(target.innerHTML);
};

However, when I wanted to keep track onkeyup, things weren't working as I thought they would be. Here is a fiddle.

What am I doing wrong?

Community
  • 1
  • 1
ayjay
  • 177
  • 1
  • 2
  • 7

1 Answers1

1

Make a habit of checking the console output each time your JS code behaves unexpectedly: too many times you'll find the explanation right there waiting for you.

In this particular case your script is not even processed completely. Because of the typo (there's no such function as document.getElementByID, the correct one is called document.getElementById) the JS parser throws (in Chrome)...

TypeError: Object #<HTMLDocument> has no method 'getElementByID'

... and stops interpreting the rest of the code.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • Thanks! I never realized that there's a "Console Output." Now I know. Thanks for catching that silly mistake. – ayjay Sep 16 '13 at 01:05