2

I cannot figured out why I got Uncaught TypeError: Illegal invocation error when calling getAttribute from a Javascript proxy.

https://jsbin.com/raqafazixe/1/edit?html,js,console

Here's the HTML code:

<div id="a"></div>

Here's the Javascript code:

const a = document.querySelector('#a');
console.log(a.getAttribute('id'));
console.log(new Proxy(a, {}).getAttribute('id'));

And here's the output:

a
Uncaught TypeError: Illegal invocation
  • https://stackoverflow.com/questions/52075255/proxy-htmlelement and https://stackoverflow.com/questions/48535551/is-it-possible-to-use-proxy-with-native-browser-objects-htmlelement-canvas2dre Your Proxy is not an Element and can't call its target's getAttribute on its behalf. – Kaiido Aug 13 '19 at 22:44
  • 1
    Using the [solution there](https://stackoverflow.com/a/48536014/3702797): https://jsfiddle.net/4okdtzp9/ – Kaiido Aug 13 '19 at 23:00
  • 1
    Similar using `bind(target)` https://jsfiddle.net/eo5f6hry/1/ – charlietfl Aug 13 '19 at 23:06
  • Thank you so much! –  Aug 13 '19 at 23:09

0 Answers0