Search code examples
jquerystringmethods

How to revapi[ VARIABLE HERE ].revnext();?


I have this API method :

revapi17.revnext();  

that let me go to the next slide inside the SLIDER number 17.

I have 30+ sliders and I have to do something like this:

revapi1.revnext(); 
revapi2.revnext(); 
revapi3.revnext(); 
revapi4.revnext(); 

....

I want to insert a variable instead of the number "17"

This is my code:

numberArray = [14,15,16,17,18,5];
jQuery.each(numberArray , function(index, value){
  $(".div"+value).click(function(){
    revapiVALUE.revnext();
  }
}       

This is what I want to do:

Inside DIV14 do revapi14.revnext(); Inside DIV15 do revapi15.revnext();

Any ideas?


Solution

  • This is your solution, simple Google search:

    How to Call a JavaScript Function From a String Without Using eval

    Relevant part of the article:

    // function we want to run
    var fnstring = "runMe";
    
    // find object
    var fn = window[fnstring];
    
    // is object a function?
    if (typeof fn === "function") fn();
    

    EDIT

    var numberArray = [14,15,16,17,18,5];
    jQuery.each(numberArray, function(index, value) {
        $('.div' + value).click(function() {
            var fn = window['revapi' + value];
            if (typeof fn === 'object') fn.revnext();
        }
    }