jQuery inconsistent .remove by class on element with multiple classes

Posted by pedalpete on Stack Overflow See other posts from Stack Overflow or by pedalpete
Published on 2010-03-14T00:37:07Z Indexed on 2010/03/14 0:45 UTC
Read the original article Hit count: 342

Filed under:

I've got a page where messages and associated elements (responses, forwards, etc) all share a class based on the database id of the parent.

For example


 this is a message
    this is another message


    this is a comment
    this is another comment


    tim posted a new message
    sara forwarded a message to john

at times I need to remove all elements with the same id, so I originally had

    jQuery('div#'+id).remove();

but that would sometimes not remove all the ids because ids are supposed to be unique. So I added the id as a class. now I use

   jQuery('div.'+id).remove();

but this seems to be about 80% effective, and sometimes the divs aren't being removed. I'm not sure if the issue is because the div has more than one class, but I need the classes because that is how I refer to the elements when somebody clicks. For instance,

jQuery('div.message').click(function(){
     get the id, send it to the server and get the message
 });

is there something wrong I'm doing here? or is there a better way to do this?

© Stack Overflow or respective owner

Related posts about jquery-selectors