0

I'm new with JS and I have the next doubt. It's possible to use destructuring with HTML to get attributes? Actually I know this way.

var foo = document.getElementById('script1');
var bar = foo.getAttribute('var1');
var any = foo.getAttribute('var2');
<script id="script1" src="code.js" var1="var1" var2="var2">
</script>

In my understanding, the way to use object destructuring is:

{ var 1, var2 } = foo;

But actually I'm getting undefined values.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

2

Document.getElementById() returns a Element object, which has properties by itself. This Element object has a property called attributes, which is a NodeValueMap

It is possible to destructure a JavaScript object, however this is not possible for selecting the attributes of the element.

Jeffrey Devloo
  • 1,406
  • 1
  • 11
  • 20