Running function on button click
-
Hello,
I’ve created a function that I’d like to run when a user clicks on a button. I’ve created the button using the Word Press editor. I’ve added an id to the button in hopes that I could use an event lister (js) to run a function when the button is clicked. Here is the HTML:
<div class=”wp-block-button has-custom-width wp-block-button__width-100 has-custom-font-size has-huge-font-size” id=”tester”>Push Me! </div>
For intermediate testing, instead of running my function, I wrote some simple code just to change the color of the button to check if I get any response from the button. I followed the format from another similar thread. Here is my code:
add_action( ‘wp_head’, function() { ?>
<script>document.getElementById(‘tester’).addEventListener( ‘click’, function () {
document.getElementById(“tester”).style.color = “blue”;
} );</script>
<?php
});Nothing is happening when I click the button. I’m not sure what I’m missing. I don’t know if running getElementById checks all the pages on my site for the id but it seems like my button isn’t properly linked to my code.
Any insight you might have would be very helpful. Thanks!
The topic ‘Running function on button click’ is closed to new replies.