Target on click

Posted by 2x2p1p on Stack Overflow See other posts from Stack Overflow or by 2x2p1p
Published on 2011-01-10T00:50:59Z Indexed on 2011/01/10 0:53 UTC
Read the original article Hit count: 226

Filed under:
|
|

Hi guys.

Look this code:

<div>
    <a href = "#"><span>Click me MAN !</span></a>
</div>

<script type = "text/javascript">
    window.onload = function () {
        document.body.addEventListener ("click", function (event) {
            var target = event.target;

            alert ("Returns \"" + target.nodeName + "\". Just want \"" + target.parentNode.nodeName + "\"")
        }, false);
    }
</script>

You can see the element "span" inside a "link", when the "link" is clicked the current target is the same "span". How can the "event.target" return "link" instead of "span".

Thanks and no, I don't want to use "parentNode".

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about events