wil lkh
wil lkh

Reputation: 77

how to call function using var name?

I have a div that needs to move to a specific position depending on which dropdown div is clicked. I made a function for each one. the functions names corresponds with the dropdown divs id + "Arrow". I then made a var that got the id name of the clicked dropdown div and added the word arrow, this way it would match the function name. when I tried to use the var to call the function it does not work.

 var arrowName = $(this).attr('id') + "Arrow()";

    function btn1s1Arrow() {$( "#greenArrow" ).animate({top: "203px", left: "291px"}, "normal" );}
    function btn1s2Arrow() {$( "#greenArrow" ).animate({top: "345px", left: "381px"}, "normal" );}
    function btn1s3Arrow() {$( "#greenArrow" ).animate({top: "444px", left: "391px"}, "normal" );}

   //////dont mind this $("#qwrapper .accordionContent2").slideUp('normal'); 
   ////////dont mind this $(this).next().slideDown('normal');

    arrowName();

help would be much appreciated. thanks in advanced... i am so stuck

here is a portion of the code, so what i said actually makes sense http://jsfiddle.net/vrs9j/1/

Upvotes: 0

Views: 168

Answers (2)

Esailija
Esailija

Reputation: 140210

Well there is no need for that, you could do this:

var arrowName = $(this).attr('id');

var animProps = {
    btn1s1: [203, 291],
    btn1s2: [345, 381],
    btn1s3: [444, 391]
};

var animProp = animProps[arrowName];
$("#greenArrow").animate({top: animProp[0], left: animProp[1]}, "normal");

Upvotes: 3

putvande
putvande

Reputation: 15213

You can do something like :

window[arrowName]();

Upvotes: 1

Related Questions