-2

If I try the code below on the console in Chrome, it is working. But the code is not working in my Javascript extension in Chrome.

var region = document.getElementById("physicalDeliveryOfficeName");
region.removeAttribute("onchange");

Any thoughts what am I doing wrong? Tried to add this line in my function and on the top of my script but it is not working.

Aldrin Ramirez
  • 129
  • 1
  • 2
  • 12
  • Is that script injected into the page, or is it executing in the extension's private environment, and also running after the element exists in the dom – Patrick Evans Jul 04 '17 at 19:06
  • probably the extension script running before the DOM load ? – Suresh Atta Jul 04 '17 at 19:06
  • It should work if correct ID and after load. removeAttribute("onchange") works in chrome: https://jsfiddle.net/mplungjan/4zqzdupa/ – mplungjan Jul 04 '17 at 19:07
  • @PatrickEvans the script that I'm trying to remove is in the page itself – Aldrin Ramirez Jul 04 '17 at 19:09
  • You probably should consider tagging the question with [tag:google-chrome-extension] before it's too late. – ibrahim mahrir Jul 04 '17 at 19:11
  • @ibrahimmahrir thank you! – Aldrin Ramirez Jul 04 '17 at 19:24
  • Please [edit] the question to be on-topic: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this almost always means including your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Jul 07 '17 at 09:51

2 Answers2

1

This is due to the execution environment that it is isolated in a Chrome extension.

You have to inject the code into the page to do modify the page context handlers.

Please see Insert code into the page context using a content script

loretoparisi
  • 15,724
  • 11
  • 102
  • 146
0

It works in console because the dom has loaded. Wrap your code in an event handler.

document.addEventListener("DOMContentLoaded", function(event) { 
  var region = document.getElementById("physicalDeliveryOfficeName"); 
  region.removeAttribute("onchange");
});

https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded

theRemix
  • 2,154
  • 16
  • 16