const button = document.querySelector("button");
const potentiallyUnsafeString = "alert(1)";
button.setAttribute("onclick", potentiallyUnsafeString);Element: setAttribute() method / notebook
Element: setAttribute() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<div>
<button id="reset" type="button">Reset</button>
<button id="toggle_disabled">Toggle</button>
</div>
<button id="hello_button">Some Text</button>