# How to Set and Get CSS Variables (var()) in HTML and JS

# How to Set and Get CSS Variables (var()) in HTML and JS by zhangxinxu from https://www.zhangxinxu.com/wordpress/?p=8163 This article may be reproduced in full. Personal websites do not need permission, provided they retain the original author, source, and links in the article....

Code: Dispatching a CustomEvent

const event = new CustomEvent('show', {
  detail: { message: payload, sentAt: new Date().toLocaleTimeString() }
});

window.dispatchEvent(event);
Result
Waiting for an event...

Code: Listening for the Event Payload

window.addEventListener('show', (event) => {
  render(event.detail.message, event.detail.sentAt);
});
Event log
No events yet.