I want to add a button on my wordpress page. When the user click on it, the text box above that button show a random number between 1-10000

I am quite new to Wordpress, I want to integrate some scripts to my Wordpress site to do this random number generation. For example I can call a php function when clicking that button. And the php code helps generate that random number and put it on the text box. but I don’t know where to begin. I googled that, but there isn’t a good reference for beginners at all

2 Answers 2


all thing you have to do is use javaScript function

JavaScript Example:

var random_number = function(){
    return Math.floor(Math.random() * 10000) + 1;
document.getElementById('button_text').onclick = function () {
    document.getElementById("input_box").innerHTML = random_number();

jQuery Example:


In Jquery we can written as,

jQuery('body').on('click', '#button_text',function(){
    var minNumber = 1;
    var maxNumber = 10000;
    var randomNumber = randomNumberFromRange(minNumber, maxNumber);
    jQuery('#input_box').val() = randomNumber;

function randomNumberFromRange(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.