fill-btn
and color-div
).</body>
tag to listen for the button click and change the div’s background color.To handle an onClick function in Webflow that fills the background color of a new div
using JavaScript, you need to use a custom script with event listeners and DOM manipulation.
Fill Color Button
with a unique ID like fill-btn
).color-target
) or a unique ID (e.g., color-div
) if it doesn’t already exist.fill-btn
.color-div
or a class you can target.tag section to capture the button click:
<script> document.addEventListener("DOMContentLoaded", function () { const button = document.getElementById("fill-btn"); const targetDiv = document.getElementById("color-div"); button.addEventListener("click", function () { if (targetDiv) { targetDiv.style.backgroundColor = "#3498db"; // change to any color } }); });</script>
document.querySelector(".color-target")
if you're using a class instead of an ID.#3498db
, red
, rgb(0, 255, 0)
).Add a button and target div in Webflow, assign unique IDs, and use a custom JavaScript snippet to fill the div’s background color when the button is clicked. Add the code in the page’s custom code section before the closing </body>
tag for it to work correctly.