tags:

views:

44

answers:

2

How can change the following JQuery code so I can use class selectors instead of id selectors so I can use class="add" and class="container" instead of id="add" and id="container"

Here is the JQuery code.

  $(function(){
      $('a#add').click(function(){
        $('#container').slideToggle('slow');
        // prevent default action
        return false;
      });
    });

I got it to work thanks everyone for the help.

+2  A: 

use a . not a #

Kyle
And by the way, Using IDs isn't bad, it's actually a much quicker selector then classes, plus there could be the issue of having two classes and one shouldn't be animated.
Kyle
I changed the # signs but it still didn't work?
alpha
Are you sure they are classes in HTML? Did you load jQuery? Is slideToggle an existing function?lol you need to put up more code so I can get some context as to what your issue is. Could be that you're using a $(function() { instead of a $(document).ready(function() { to begin with.
Kyle
It works with the id selectors but not with the class selectors:(
alpha
Then why not just use the ID tags? =)
Kyle
+3  A: 

JS

$('a.add').click(function(){ // Use . (dot) for class select
    $('.container').slideToggle('slow');
    return false;
});

HTML

<a href="#" class="add">Add container</a>
<div class="container">
  My container content
</div>
sshow