0

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.

6 Answers 6

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

I'm guessing there's only one instance of each span on the page. If not, you can modify this slightly to get something that behaves the same way:

// Newlines added for readability
$('span[rel=color_content],
   span[rel=color_link],
   span[rel=color_selected],
   span[rel=color_page],
   span[rel=color_player]').ColorPicker({
       onChange: function(hsb, hex, rg){
           $('span[rel='+$(this).attr('rel')+']')
               .css('background-color', '#' + hex);
       }
   });
Sign up to request clarification or add additional context in comments.

3 Comments

The OP code changes the background-color for the entire list of elements (the entire selector), not just the one that was changed
@Šime - Added a second part to the answer to address that (even though I don't think that is the intent).
1. Maybe $('span').filter('[rel=foo], [rel=bar], ...') 2. this.rel is safe, no need for attr()
5

You could do something like:

function setColorPicker(css_selector) {
    $(css_selector).ColorPicker({
      onChange: function (hsb, hex, rg) {
         $(css_selector).css('background-color', '#' + hex);
      }
    });
}

Comments

0
for(i in ['content','link','selected','page','player'])
{
    $("span[rel=color_"+i+"]").ColorPicker({   
    onChange: function (hsb, hex, rg) {$("span[rel=color_"+i+"]").css('background-color', '#' + hex);   } }); 
}

Comments

0

If you end up with a really long list of these to maintain, one way is to put all the changing information in one easily-maintainable variable:

var rels = ['color_content','color_link','color_selected','color_page'];

Then you could have:

// build the selectors according to your patterh
var selectors = $.map( rels, function(e,i){ 
  return 'span[rel=' + rels + ']'; 
}).join(',');

$(selectors).ColorPicker({
  onChange: function(hsb,hex,rg){
    $(this).css('background-color','#'+hex); 
});

Comments

0
var arr = [
    'color_content',
    'color_link', 
    'color_selected', 
    'color_page', 
    'color_player'
];

function foo() {
    var i, selector;

    for (i = 0; i < arr.length; i++) {
        selector = 'span[rel=' + arr[i] + ']';

        $(selector).ColorPicker({
            onChange: function (hsb, hex, rg) {
                $(selector).css('background-color', '#' + hex);
            }
        });
    }
}

Comments

-1

You can separate several CSS selectors using a comma:

$("span[rel=color_player], span[rel=color_page]").ColorPicker();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.