javascript - How to write repetitive [onClick] statements into single line in JQuery? -
how write following statements 1 line?
$("#pushkar").on("click", function () { gula.hideunhide("#pushkardiv", "#pushkar", "#pushkar"); }); $("#rashkar").on("click", function () { gula.hideunhide("#rashkardiv", "#rashkar", "#rashkar"); }); $("#kashkar").on("click", function () { gula.hideunhide("#kashkardiv", "#kashkar", "#kashkar"); }); $("#fuskar").on("click", function () { gula.hideunhide("#fuskardiv", "#fuskar", "#fuskar"); }); $("#ashke").on("click", function () { gula.hideunhide("#ashkediv", "#ashke", "#ashke"); }); i have around 20 statements above , consuming lot of space.
i told stop repetition , make 1 line all; accumulate each button id.
is better strategy write in single line? not harms readability?
try this:
$("#pushkar, #rashkar, #kashkar, #fuskar, #ashke").on("click", function () { var id = "#" + this.id; gula.hideunhide(id + "div", id, id); }); btw, better keep common class. can this:
$(".commonclass").on("click", function () { var id = "#" + this.id; gula.hideunhide(id + "div", id, id); }); note: sending same argument twice seems unnecessary kept way only.
just suggestions you, there seems no need of passing 3 arguments. having single argument work great. example:
hideunhide(id){ var divid = id + "div"; // other code here } and later in click events:
gula.hideunhide("#" + this.id);
Comments
Post a Comment