0

My code:

$('.play').click(function () {
 //HERE IS ACTION
//I WANT TO CALL RANDOM FUNCTION FROM ARRAY
        });


function testone(){
alert("WORK F 1111");
}

function testtwo(){
alert("WORK F 2222");
}


function testthree(){
alert("WORK F 3333");
}


var testarray = ["testone();","testtwo();","testthree();"]

Anyone can help me with this, i don't know how to call function from array. Thanks :)

1
  • Can you explain that more? And example. Commented Dec 14, 2015 at 17:41

1 Answer 1

1

reference the actual function, don't use strings, and it's quite easy

$('.play').click(function () {
   var random = testarray[Math.floor(Math.random() * testarray.length)];

    random();
});


function testone(){
    alert("WORK F 1111");
}

function testtwo(){
    alert("WORK F 2222");
}

function testthree(){
    alert("WORK F 3333");
}

var testarray = [testone, testtwo, testthree];

$('.play').click(function () {
   var random = testarray[Math.floor(Math.random() * testarray.length)];

    random();
});


function testone(){
    alert("WORK F 1111");
}

function testtwo(){
    alert("WORK F 2222");
}

function testthree(){
    alert("WORK F 3333");
}

var testarray = [testone, testtwo, testthree];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="play">Click Me</div>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.