I have an HTML element that contains a data attribute:
<a href="#" data-trigger="{ "rem": "albatros", "ap":1 }'">Remove</a>
<div data-container>
<p>lorem ipsum<p>
<p data-rem></p>
</div>
1.
Sometimes (but not always) a value is sent in the data-trigger
attribute:
I am trying to select all elements that have the attribute data-trigger
:
document.querySelectorAll(['data-trigger']).forEach(function (trigger, index) {
Then, for each of these trigger
elements, I aim to retrieve the DOM and JSON value and parse it:
dom = trigger;
value = JSON.parse(trigger.getAttribute('data-trigger'));
Although I successfully retrieve the DOM
reference, I always end up with a null
value.
Does using getAttribute
trigger a reevaluation in the DOM?
Iterating through the
data-container
, I search for elements that contain attributes corresponding to one of the keys found in the JSON.parse result. I then proceed to set the value of these elements to the value of the key.
For instance:
<p data-rem>albatros</p>