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

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -