SHARE

How to insert onclick function in wordpress posts

onclick function is a javascript function. It is used to execute some code or some function on onclick function run. WordPress posts and pages are designed only for insert text.This is the reason when onclick function is added to button or lable. It is trimmed by wordpress automaticly.

Here i show simple technique to insert onclick function in wordpress posts and pages. To insert onclick function in wordpress posts and pages follow the steps.

onclick function wordpress
onclick function wordpress

Download and install insert php plugin

Firstly download and install some plugin to run php under post.In my case i installed “Allow PHP in post” plugin.After install the plugin you can insert php in wordpress posts.

You can use any other plugin also to insert php in post. Now create a php code and insert script or onclick function in the php code. Example i have a script and code

<button onclick="w3cValidator()">Validate</button>
<a id="demo" target="_blank" href="https:tectrick.org"></a>
<script>
function w3cValidator() {
var x = document.getElementById("myText").value;
document.getElementById("demo").href="https://validator.w3.org/check?uri="+ x;
document.getElementById("demo").click();
}
</script>

Place the script and onclick function in php code

In this code button tag has a onclick function. Place this code under php code.

<?php
echo '<button onclick="w3cValidator()">Validate</button>
<a id="demo" target="_blank" href="https:tectrick.org"></a>
<script>
function w3cValidator() {
var x = document.getElementById("myText").value;
document.getElementById("demo").href="https://validator.w3.org/check?uri="+ x;
document.getElementById("demo").click();
}
</script>' ; ?>

Place this code in your plugin which you want to use insert php in posts. With “echo” the script and code you can enable to use onclick function under posts and pages.If you have question about the post and question bellow.