if statement - How to shorten the same part in jQuery function? -
if have if/else statement:
if(a) { $('#aa').removeclass('offselect').addclass('onselect').css('background-image','url(../images/menu.gif)'); } else if(b) { $('#bb').removeclass('offselect').addclass('onselect').css('background-image','url(../images/menu.gif)'); } else if(c) { $('#cc').removeclass('offselect').addclass('onselect').css('background-image','url(../images/menu.gif)'); } else if(d) { //same part }
you can see code have same part:
removeclass('offselect').addclass('onselect').css('background-image','url(../images/menu.gif)');
is possible or suggestion shorten same code?
use function reused code :
if(a) { // pass jquery selector reference doprocess( $('#aa') ); } else if(b) { doprocess( $('#bb') ); } else if(c) { doprocess( $('#cc') ); } else if(d) { //same part } function doprocess( e ){ // use e selector e.removeclass('offselect').addclass('onselect').css('background-image','url(../images/menu.gif)'); }
Comments
Post a Comment