Laurent Laurent - 1 year ago 67
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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download