Inside your click event, none of your selectors refers to the actual click. That is, if you press the second .switchonOff .switchButton , most likely your selectors will find the first element (top). (for example, $('.switchButton').attr('rel') will always be retrieved from the first element on the page, even though the event is fired from the second element)
To fix this, make sure that you associate your searches with the item itself. this can be done by providing a scope parameter:
var feedbackOn = 'ON'; $('.switchOnOff .switchButton').click(function(){ // here your scope var $scope = $(this).closest('.switchOnOff'), rel = $(this).attr('rel'); // now locate the elements you want from within scope: if (rel == 'off'){ $(this).attr('rel','on'); $scope.find('.switchLed').removeClass('off').addClass('on'); $scope.find('.switchFeedback').html(feedbackOn); }else if (rel == 'on'){ $(this).attr('rel','off'); $scope.find('.switchLed').removeClass('on').addClass('off'); $scope.find('.switchFeedback').html('OFF'); } return false; });
Demo
screenshot of http://img811.imageshack.us/img811/2027/40913258.png
You can also refer to .removeClass to remove the on / off classes used when they are opposite. Unaware of my intentions, I did not add it, but thought that I wanted to mention it if he wanted to.
source share