artparks
artparks

Reputation: 761

Variable used in function name

I'm making a helper function that, amongst other things adds or removes a class. The function needs to be able to do either as there doesn't seem any point in making two functions with the only difference being one adds a class the other removes it. So:

function kalf_addRow( type, action ) {
    found = $("."+type).not(".row-vis").first();
    found.slideDown();
    $(found).[INSERT 'action' IN HERE]Class("row-vis");     
}

I would pass it either "add" or "remove" as the "action" parameter and then need it inserted where shown. I would need to do similar for the slideDown too but it's basically the same problem.

Upvotes: 2

Views: 77

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190907

As asawyer said, you could use toggleClass.

$(found).toggleClass("row-vis", action == 'add');    

Or another way would be to use an if. Or you could do this

$(found)[action + 'Class']("row-vis");    

Upvotes: 4

Related Questions