Why does the author of Test-Driven JavaScript Development (Christian Johansen) use the while statement instead of the if statement in the code below?
function getEventTarget(event) {
var target = event.target || event.srcElement;
while (target && target.nodeType != 1) {
target = target.parentNode;
}
return target;
}
Because the author wanted to keep walking up the tree until the correct node type was found; it might not be the immediate parent.
However, in this case it makes no sense, as parentNode will always return an element in real-world usage (or a Document).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With