Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Anchor Tag With Italic Tag

I have some html.

<a href="#">
   <i class="some-bg" />
   Some Text
</a>

And some Javascript.

$("a").bind("touchstart", function (e) {
   e.preventDefault();

   console.log("Tag: " + e.target);
   console.log("Tag Name: " + e.target.tagName);
});

The response is.

Tag: [object HTMLElement]
Tag Name: I

Why? Shouldn't it be anchor?

UPDATED

$("a, a *").bind(function() {
    e.stopPropagation();

    // other stuff
});

Will this do the trick?

like image 418
Umair A. Avatar asked Sep 12 '11 16:09

Umair A.


1 Answers

Why?

Because you touched the <i> (and then the event bubbled up to the <a>).

Shouldn't it be anchor?

No. Use currentTarget if you want the element to which the event is bound rather then the one which actually triggered the event.

like image 93
Quentin Avatar answered Oct 05 '22 13:10

Quentin