You could do this too: var $buttons = $('.button'); $buttons.click(function(){ $buttons.not(this).css('background-color', 'green'); });
On Wed, Oct 28, 2009 at 05:39, alexpls <alex...@gmail.com> wrote: > > I'm a beginner to programming, so please bear with me, I might be > missing something really obvious here and not realising it. > For my first jQuery script that I'm writing unassisted by a tutorial, > I want to make a very simple page. > This page has three squares on it, and once you click on a square it > changes to blue while the other two squares change to green. > To avoid code repetition, and give myself a bit of a challenge I > decided to use the switch statement, and have implemented it like > this: > > $(document).ready(function(){ > > $(".button").click(function(){ > var id = "#" + $(this).attr("id"); > > switch(id){ > case id: > var formattedID = "\'" + id + "\'"; > > $(".button:not(formattedID)").css("background-color", "green"); > $("#test").append("formattedID = " + > formattedID + "<br />"); > break; > }; > > }); > > }); > > What seems to make this code not work is the :not statement. If I > replace the ":not(formattedID)" with ":not('#button1')" the code works > as expected. I don't understand why the :not() works with a string > that's directly typed into it, but not with a variable that holds a > string. > > I've uploaded this to JS Bin, so you can see the code fully. > > http://jsbin.com/ibate/edit > > Any help is very appreciated, > > alexpls >