To handle an onClick function in Webflow that fills the background color of a newly created div block using JavaScript, follow these steps.
create-div-button) via the Element Settings panel.
<div> and set its background color.
Example:
<script>
document.addEventListener('DOMContentLoaded', function () {
const button = document.getElementById('create-div-button');
button.addEventListener('click', function () {
const newDiv = document.createElement('div');
newDiv.style.width = '200px';
newDiv.style.height = '200px';
newDiv.style.backgroundColor = '#3498db'; // Blue color
newDiv.style.marginTop = '20px';
document.body.appendChild(newDiv);
});
});
</script>
<div> element, applies a fixed background color, and appends it to the <body>.
Add a unique ID to your button in Webflow, then use JavaScript via a custom embed to create and style a new div on click. Make sure all scripts are run after DOMContentLoaded to ensure elements are available for manipulation.