Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.currentTarget.innerHTML is empty in IE10 after view is re-rendered

Here is the JSBin Demo (Please test in Chrome and IE10 to see the difference)

HTML

<body>
  <div id='a'>
    <button>CLick me</button>
  </div>
</body>

JS

var markup = '<button>CLick me</button>';

$('#a').on('click', 'button', function() {
  $('#a').html(markup);
});

$('#a').on('click', 'button', function(event) {
  console.log(event.currentTarget.innerHTML); //.....(1)
});

Line (1) gives correct output in Chrome and Firefox while in IE10 I get empty string as the value of event.currentTarget.innerHTML. It only happens when div(#a) is rendered again.

PS: I am using Backbone.js in which I re-rendered the view but to make question simpler, I have refined the problem to above mentioned by taking backbone out of picture.

like image 295
Sachin Avatar asked Feb 18 '14 08:02

Sachin


People also ask

Why is event currentTarget null?

currentTarget is only available while the event is being handled. If you console. log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null .

What is event currentTarget return?

The currentTarget event property returns the element whose event listeners triggered the event. This is particularly useful during capturing and bubbling.

What is the difference between event target and event currentTarget?

target is the element that triggered the event (e.g., the user clicked on) currenttarget is the element that the event listener is attached to.

Why is event target undefined?

You're likely getting this error because you're trying to get a name attribute on elements that don't have a name attribute. For example; input, textarea, and form elements are naturally able to get a name attribute. But elements like div, span doesn't.


1 Answers

I was curious so had a go. This won't answer your question but might point in the right direction

(1) In your example you replace a button with an identical button, if you replace it with different text you'll see in chrome that the innerHTML is actually the unreplaced text

(2) In IE currentTarget is an empty button with no parent

(3) It (sort of) works in IE and chrome if you do this:

event.delegateTarget.children[0].innerHTML

(4) doc type makes no difference either way

So your answer will be in finding the difference between delegateTarget and currentTarget, or just using delegateTarget

like image 105
tony Avatar answered Oct 05 '22 20:10

tony