Laurent Laurent - 9 months ago 33
HTML Question

Javascript - how avoid this multiples functions for same propose?

I am writing a set of JavaScript functions and was wondering if I could make a function simpler (e.g. a more global function)?

What I have is a set of 5 toggles for which I would like to write a function

. What I wrote so far is :

function toggleOn1() {

function toggleOff1() {

function toggleOn2() {

function toggleOff2() {

function toggleOn3() {

function toggleOff3() {

// etc.


Pass the Jquery selector to the function

function toggle(selector,state){

Both the selector and state will be strings.

You can call it like this