Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: selecting by class after class change by .addClass() or .attr()

Tags:

jquery

I have some jQuery code something like this:

$(document).ready(function() {
    $("img.off").click(function() {
        alert('on');
       $(this).attr('class', 'on'); 
    });
    $("img.on").click(function() {
        alert('off');
        $(this).attr('class', 'off'); 
    });
});

The selector works fine for images that have the class name defined in the original HTML document, however after manipulating the class name with jQuery, the img item will not respond to selectors using it's new class.

In other words, running the above code, if you click an 'off' img, it will trigger the first function, and change the class to 'on'. However, clicking this image again does not trigger the second function (as I would have expected), but rather triggers the first again. It's as if the selector is reading the old DOM rather than the updated version. What am I doing wrong here?

Firefox 3.6.3 - jQuery 1.4.2

like image 992
UpTheCreek Avatar asked May 25 '10 12:05

UpTheCreek


People also ask

What does addClass do in jQuery?

jQuery addClass() Method The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

How can I replace one class with another in jQuery?

To replace a class with another class, you can remove the old class using jQuery's . removeClass() method and then add the new class using jQuery's . addClass() method.

How do I select a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.


3 Answers

You can use .live() to do what you want, like this:

$(function() {
  $("img.off").live('click', function() {
    alert('on');
    $(this).attr('class', 'on'); 
  });
  $("img.on").live('click', function() {
    alert('off');
    $(this).attr('class', 'off'); 
  });
});

When you do $(selector).click() you're finding the elements that match at that time and binding a handler to the click event...when their class changes later it doesn't matter for this, the handler is attached. .live() works differently, actually caring about the selector matching when the event happens.

Also, depending on your example/what you're ultimately after, something like .toggleClass() might simplify it for you, like this:

$(function() {
  $("img.off, img.on").live('click', function() {
    $(this).toggleClass('on off'); 
    alert($(this).attr('class'));
  });
});
like image 128
Nick Craver Avatar answered Oct 17 '22 01:10

Nick Craver


Try with live():

$(document).ready(function() {
    $("img.off").live('click', function() {
        alert('on');
       $(this).attr('class', 'on'); 
    });
    $("img.on").live('click', function() {
        alert('off');
        $(this).attr('class', 'off'); 
    });
});
like image 45
Sarfraz Avatar answered Oct 17 '22 01:10

Sarfraz


You need to either rebind the click callback after you change the class, or use .live()

like image 26
digitaldreamer Avatar answered Oct 16 '22 23:10

digitaldreamer