How to refactor jquery

Posted by Pierce McGeough on Stack Overflow See other posts from Stack Overflow or by Pierce McGeough
Published on 2013-11-06T09:25:20Z Indexed on 2013/11/06 9:53 UTC
Read the original article Hit count: 118

Filed under:
|

I have buttons on the page that will activate and deactive settings. the Ids are the same bar their prefix e.g I have '#rl-activate', '#rl-deactivate', '#cl-activate', '#cl-deactivate' Is there a way to refactor this code so i am not doing it for every button on the page.

// rl activate
$('#rl-activate').click(function(){
  $('#rl-activate').hide();
  $('#rl-deactivate').show();
  $('#rl').val(50).prop('selected', true);
  $('#rl').prop('disabled', false).trigger('liszt:updated');
  displayCPM();
  newPrice();
  checkSettings();
});

// rl deactivate
$('#rl-deactivate').click(function(){
    $('#rl-deactivate').hide();
    $('#rl-activate').show();
    $('#rl').prop('disabled', true).trigger('liszt:updated');
    $('#rl').val('').trigger('liszt:updated');
    displayCPM();
    newPrice();
    checkSettings();
});

So for the next one all that changes will be the rl to cl to bm etc

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about refactoring