Using JavaScript with Gutenberg Button
-
I created two(2) sections using Gutenberg. Now, there’s a button I will like to add JavaScript to.
I want to hide a Block by default, with a button under it that says “Show More”.
If the button is clicked, the hidden Section should appear, and the button Text should change to “Show Less”.
If the Button is clicked again, the visible Section should become hidden again, and the Button Text should say “Show More”.
So when hidden, the button says “Show More”, and when visible the button says “Show Less”.
Here’s my JavaScript below:
===========================================================
function toggleBlock() {
// get the block
var myBlock = document.getElementById(‘service-area-two’);// get the current value of the block’s display property
var displaySetting = myBlock.style.display;// also get the block button, so we can change what it says
var blockButton = document.getElementById(‘Show More’);// now toggle the block and the button text, depending on current state
if (displaySetting == ‘block’) {
// block is visible. hide it
myBlock.style.display = ‘none’;
// change button text
blockButton.innerHTML = ‘Show Less’;
}
else {
// block is hidden. show it
myBlock.style.display = ‘block’;
// change button text
blockButton.innerHTML = ‘Show More’;
}
}==========================================
The BLOCK ID: service-area-two
The Button Name is: Toggle ButtonI know I’m doing a lot of things wrong.
How can I get this button to work with JavaScript?
Regards
The topic ‘Using JavaScript with Gutenberg Button’ is closed to new replies.