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 :)
asked Dec 14, 2015 at 17:37
Karol Jankiewicz
1531 silver badge12 bronze badges
-
Can you explain that more? And example.Karol Jankiewicz– Karol Jankiewicz2015年12月14日 17:41:40 +00:00Commented Dec 14, 2015 at 17:41
1 Answer 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>
answered Dec 14, 2015 at 17:40
adeneo
319k29 gold badges410 silver badges392 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default