0

i have the following html code and i want to add on click the class "rotate" (which i have included in my css) to the div with class "barklogo"

<div class="barklogo loaded" data-width="40" data-anim="left">
<img src="/images/home/button.png" border="0" alt="Demo image">
</div>

I found a similar post here

JQUERY - add CSS class to BUTTON element after click

But when i enter the following code to the js file it won't work

 $(document).ready(function(){
  $('barklogo').click(function(){
       $(this).addClass('rotate');
  });
 }); 

Here is the css code for the class .rotate

.rotate{
transform: rotate(360deg);
-webkit-transform: rotate(360deg);
-moz-transform: rotate(360deg);
-o-transform: rotate(360deg);
-ms-transform: rotate(360deg);
}

Thank you very much!

5 Answers 5

3

You have to use . with class name in jquery.

 $('.barklogo').click(function(){

Class Selector (“.class”)

Sign up to request clarification or add additional context in comments.

Comments

1

You can also do this: Use . when u are using class selector

$(document).ready(function(){
  $('.barklogo .loaded').click(function(){
       $(this).addClass('rotate');
  });
 }); 

1 Comment

Your selector is wrong. $('.barklogo .loaded') finds all elements that have class loaded that are descendants of an element that has class barklogo.
1

You have missed to add . in selector,

    $(function(){
      $('.barklogo').click(function(){
           $(this).addClass('rotate');
      });
     });

Comments

0

You missed . for class selector in jquery

Change $('barklogo').click(function(){ to $('.barklogo').click(function(){

Comments

0

Its really working by this way. But if u want in safe way to addClass which already have more than one class in div than use addClass to div by its ID. for example,

$("#div_id").click(function(){
    //code 
});

This is your useful way for this question.

<script>

$('.barklogo').click(function(){
           $(this).addClass('rotate');
      });

</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.