1
<div
    data-global-messages="{&quot;addCartSuccessMessage&quot;:&quot;successfully added&quot;,&quot;creditRepresentativeMessage&quot;:&quot;Please contact your Credit Representative&quot;,&quot;nonStockMessage&quot;:&quot;Non-stock item&quot;,&quot;addWishlistSuccessMessage&quot;:&quot;Wish List.&quot;,&quot;itemExistsCartMessage&quot;:&quot;Please review error(s) below.&nbsp;&quot;,&quot;itemExistsWishlistMessage&quot;:&quot;Please review error(s) below.&nbsp;&quot;,&quot;nonAvailableMessage&quot;:&quot;substitute.&quot;,&quot;creditDepartmentMessage&quot;:&quot;Please contact Sea.&nbsp;&quot;}">
  </div>

How to get the attributes from above?

I tried document.querySelector('data-global-messages').getAttribute('addCartSuccessMessage') which returns null

0

1 Answer 1

1

All data attributes are stored in the element's dataset property. To access it you can do:

const element = document.querySelector('div[data-global-messages]');
const data = element.dataset.globalMessages;
const json = JSON.parse(data);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.