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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -