var h1 = document.querySelector("h2"); h1.addEventListener("input", function() { this.setAttribute("data-heading", this.innerText); });