Reputation: 19
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!
Upvotes: 0
Views: 811
Reputation: 71
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>
Upvotes: 0
Reputation: 22711
You have missed to add .
in selector,
$(function(){
$('.barklogo').click(function(){
$(this).addClass('rotate');
});
});
Upvotes: 1
Reputation: 3600
You can also do this:
Use .
when u are using class selector
$(document).ready(function(){
$('.barklogo .loaded').click(function(){
$(this).addClass('rotate');
});
});
Upvotes: 1
Reputation: 7762
You missed .
for class selector in jquery
Change $('barklogo').click(function(){
to $('.barklogo').click(function(){
Upvotes: 0
Reputation: 133403
You have to use .
with class name in jquery.
$('.barklogo').click(function(){
Upvotes: 3