Answers for "Using DOM Nodes As Keys"

0

Using DOM Nodes As Keys

<span id="thing" class="thing">a thing.</span>

<script>
const myWeakMap = new WeakMap();

// Set a value to a specific node reference.
myWeakMap.set(document.getElementById('thing'), 'some value');

// Access that value by passing the same reference.
console.log(myWeakMap.get(document.querySelector('.thing')); // 'some value'
</script>
Posted by: Guest on October-08-2021
0

Using DOM Nodes As Keys

<span id="el1">first element</span>
<span id="el2">second element</span>

<script>
  const someObj = {
    [document.getElementById('el1')]: 'some value'
  };

  console.log(someObj[document.getElementById('el1')]);
  // 'some value'
</script>
Posted by: Guest on October-08-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language