How to invoke a function on clicking a div?

I want a function to be invoked when I click on a div.

I have the functions arti1(),arti2(),arti3(). So, if I click that div, the function on that div as follows:

<div class="random" onclick="rondom()">Random</div></div>

    function rondom(){

        var rndm = Math.floor(Math.random() * 8) + 1;
        arti+rndm+().css("display", "block");
        }

How can I display that random function?

Upvotes: 0

Views: 56

Answers (2)

matrmawi
matrmawi

Reputation: 121

You can use eval () or put your functions inside an array

 function rondom(){

   var rndm = Math.floor(Math.random() * 8) + 1;
    eval ('arti'+rndm+'().css("display", "block")');
    }

Upvotes: 0

user4109793
user4109793

Reputation:

<div class="random" onclick="rondom()">Random</div></div>

function rondom(){

    var rndm = Math.floor(Math.random() * 8) + 1;
    eval("arti"+rndm+"().css(\"display\", \"block\")");
    }

Now it works!

Upvotes: 1

Related Questions