Refactor: Javascript block

Posted by Shpigford on Stack Overflow See other posts from Stack Overflow or by Shpigford
Published on 2011-01-10T18:45:55Z Indexed on 2011/01/10 18:53 UTC
Read the original article Hit count: 188

Filed under:
|
|

Need some help refactoring this code:

$("span[rel=color_content]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_content]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_link]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_link]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_selected]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_selected]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_page]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_page]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_player]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_player]").css('background-color', '#' + hex);
  }
});

The only thing changing between each is the contents of the rel attribute.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery