I am a newcomer to jQuery and am working on an app that allows the user to paint images. To do that, I've created a color palette, and if a user clicks on a color, the paint brush successfully changes to that color. However, my code looks really ugly. I really want to clean it up.
$(document).ready(function() {
$('#color-one').click(function() {
brush.setColor('#990000');
});
$('#color-two').click(function() {
brush.setColor('#CC0000');
});
$('#color-three').click(function() {
brush.setColor('#CC3300');
});
$('#color-four').click(function() {
brush.setColor('#FFCC00');
});
$('#color-five').click(function() {
brush.setColor('#FFFF00');
});
$('#color-six').click(function() {
brush.setColor('#CCFF00');
});
$('#color-seven').click(function() {
brush.setColor('#66FF00');
});
$('#color-eight').click(function() {
brush.setColor('#003300');
});
$('#color-nine').click(function() {
brush.setColor('#33FFCC');
});
$('#color-ten').click(function() {
brush.setColor('#3300CC');
});
$('#color-eleven').click(function() {
brush.setColor('#660033');
});
$('#color-twelve').click(function() {
brush.setColor('#660066');
});
$('#color-thirteen').click(function() {
brush.setColor('#000000');
});
$('#color-fourteen').click(function() {
brush.setColor('#ffffff');
});
});
</script>
HTML
<div id = "colors">
<div id="color-one" style="background: #990000;"></div>
<div id="color-two" style="background: #CC0000;"></div>
<div id="color-three" style="background: #CC3300;"></div>
<div id="color-four" style="background: #FFCC00;"></div>
<div id="color-five" style="background: #FFFF00;"></div>
<div id="color-six" style="background: #CCFF00;"></div>
<div id="color-seven" style="background: #66FF00;"></div>
<div id="color-eight" style="background: #003300;"></div>
<div id="color-nine" style="background: #33FFCC;"></div>
<div id="color-ten" style="background: #3300CC;"></div>
<div id="color-eleven" style="background: #660033;"></div>
<div id="color-twelve" style="background: #660066;"></div>
<div id="color-thirteen" style="background: #000000;"></div>
<div id="color-fourteen" style="background: #ffffff;"></div>
</div>
I was thinking that the refactored version might look something along the lines of this:
$('#color-one, #color-two, ... color-fourteen).click(function() {
brush.setColor(this.val);
});
However, I would like to see what this community has to think.