App for painting images
You are right.  You have way too much duplication there.  And you are on the right track with your refactored version but I don't think it goes far enough.

Right now if you want to add a new color you have to do the following.


Add a new div to the color buttons with a unique id.
Add style rules (background) to that div with the color.
Add a button click rule that selects the button by id.
Change the brush color making sure it matches the background done in #2.


With your refactoring you would change this to:


Add a new div to the color buttons with a unique id.
Add style rules (background) to that div with the color.
Add a value to the div button.
Add the id to the button click function.


So you might have made the code shorter but didn't really make it much easier to maintain.

So, how can we get this shorter.  Well, we can select elements by class name.  So instead of using the id we just use class names.

&lt;div id = "colors"&gt;
    &lt;div class="brush-selector" style="background: #990000;" data-val="#990000;"&gt;&lt;/div&gt;
    ...
    &lt;div class="brush-selector" style="background: #ffffff;" data-val="#FFFFFF"&gt;&lt;/div&gt;
&lt;/div&gt;


And your script becomes:

$('#colors .brush-selector').click(function() {
    brush.setColor(this.data('val'));
});


Now to add a new color you have the following steps:


Add a new div to the color buttons with a unique id.
Add style rules (background) to that div with the color.
Add a value to the div button that matches the color.


Better, but we are still duplicating info.  The background color and data-val fields are the same.  And if we accidentally type one wrong then we have incorrect behavior.  So why don't we just use the background-color of the div.

&lt;div id = "colors"&gt;
    &lt;div class="brush-selector" style="background: #990000;"&gt;&lt;/div&gt;
    ...
    &lt;div class="brush-selector" style="background: #ffffff;"&gt;&lt;/div&gt;
&lt;/div&gt;


$('#colors .brush-selector').click(function() {
    brush.setColor(this.css('background-color'));
});


That takes our steps down to:


Add a new div to the color buttons.
Add style rules (background) to that div with the color.


Which is pretty good.  Basically one step to adjust colors.  Just adjust the divs.

There are further steps you could take that I won't go into detail but leave to you.

If you knew the only divs in the #colors elements were brush selectors you could remove the class altogether and just select the divs within #colors $(#colors div).  

You could also create a javascript array of allowed colors.  Then create the buttons using the array.  Then the only step you would need would be to adjust the colors in that array.  This would also allow you to select colors dynamically since that array could come from anywhere.