- This topic has 5 replies, 2 voices, and was last updated 6 months ago by Ying.
April 15, 2019 at 8:54 am #869694Ranjit Kumar
I want to add a Button on click function to run java script
<button onclick=”saveTextAsFile()” <span>Save Text to File</span></button>
when I am adding this to WordPress page is not working.
please suggest how to add button script in my page or postApril 15, 2019 at 9:09 am #869725DavidStaffCustomer Support
you’re markup is missing a
>after the onclick event – should look like this
<button onclick="saveTextAsFile()">Save Text to File</button>
– also removed the
<span></span>tags not sure why they’re there.
Adding CSS: http://docs.generatepress.com/article/adding-css/April 15, 2019 at 9:22 am #869734Ranjit Kumar
<button onclick=”saveTextAsFile()”>Save Text to File</button>
when i paste above code and publish the page it changed to <button>Save Text to File</button>April 15, 2019 at 9:47 am #869752DavidStaffCustomer Support
Thats because WordPress TinyMCE editor strips out the onclick attribute as it sees it as a possible risk. There are several solutions if you google “stop WordPress from stripping HTML”. If you’re using the Gutenberg block editor, the HTML Block is more forgiving and will ( probably ) allow it.
But… adding an onclick event in HTML is bad practice and should be avoided.
You’re better off adding an event listener in you JS eg.
const saveTextFile = document.querySelector('.save-button'); saveTextFile.addEventListener('click', saveTextAsFile);
Then add the
save-buttonclass to your button e.g
<button class="save-button">Save Text to File</button>
Adding CSS: http://docs.generatepress.com/article/adding-css/September 23, 2022 at 7:56 am #2351778Arnaud
Jumping here as my topic seems to be quite similar to this one
I would like to trigger this bit of JS when the user clicks on the ‘Book now’ CTA which is made using the ‘Button’ element
You will one the header of the website : https://wordpress-834202-2871905.cloudwaysapps.com/
How should I add the script ? Call it from funtion.php ?
How do I set the onclick action to trigger the script ? adding a specific class to the CTA ?
thxSeptember 24, 2022 at 10:52 am #2352639YingStaffCustomer Support
Does the plugin or script provider also provide a widget button? That should work to load the script.
The script can go to a hook element, and use
I don’t think you can use GB’s button for this as it doesn’t allow you to edit its HTML.
- You must be logged in to reply to this topic.