1

I'm writing a Chrome extension that injects a content script into a page. That script adds an event listener to a select element on the page so that a routine can be executed whenever someone selects a new value from the drop-down.

I've tried adding this event listener with both the "normal" JS method:

document.getElementById('mySelect').addEventListener('change', doSomething);

...and the jQuery method:

$('#mySelect').change(doSomething);

Neither seems to work; the event never fires.

By inspecting the element with Chrome's developer tools, I've discovered that the element already has an event listener on it, created by another script already running on the page. I know that this listener is functioning because the resulting script logs some data to the console. I'm wondering if this existing listener could somehow be "absorbing" the change event and preventing it from reaching my own listener.

Oddly enough, when I turn on monitorEvents for the element, it logs no events at all of any type (not even things that shouldn't be affected, like mouseover).

Any thoughts?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pacific 231
  • 181
  • 8
  • 1
    If the listener stops the immediate propagation of event by calling `e.stopImmediatePropagation()`, then subsequent listeners of same kind, that is attached won't be invoked – Vigneswaran Marimuthu May 14 '15 at 05:43
  • 1
    might be because the element is added after the event registration... can you do `alert($('#mySelect').length)` before `$('#mySelect').change(doSomething);` – Arun P Johny May 14 '15 at 05:44
  • Tried changing existing event listener ? – guest271314 May 14 '15 at 06:21
  • Thanks for the thoughts guys. @VigneswaranMarimuthu, I can't find any calls of stopImmediatePropagation. – Pacific 231 May 14 '15 at 13:31
  • @ArunPJohny, tried it and the first call returns "1", which indicates to me that the element does exist before I try to attach the listener. – Pacific 231 May 14 '15 at 13:33
  • @guest271314, Unfortunately I can't, it's part of a script that's built into the webpage I'm trying to work with. I cannot alter it. – Pacific 231 May 14 '15 at 13:34
  • what is the result of http://jsfiddle.net/arunpjohny/r7p3h5ae/2/ – Arun P Johny May 14 '15 at 13:38
  • @ArunPJohny Still doesn't work. – Pacific 231 May 14 '15 at 14:07
  • I have an update though -- I used my Chrome extension to block loading of the script that includes the existing event listener. When I do that, my original code works fine. However I can't find anything in the offending script that would block mine from working...anything else I should be looking for? – Pacific 231 May 14 '15 at 14:08
  • @Pacific231 One possibility is that when the initial change event is fired, the actual value of select is unchanged relating to second change event; that is, the value was already changed and handled by initial listener ?Another possibility http://stackoverflow.com/a/29873845/ ? – guest271314 May 14 '15 at 15:12

0 Answers0