Slaythern Aareonna
Slaythern Aareonna

Reputation: 353

Add class via jQuery

I have a #myDiv And i want, when page load, ad class to #myDiv

E.G. Page load, #myDiv.class

I use this code but it's not working:

$('#sky').addClass(‘animate-in’); 

Upvotes: 2

Views: 171

Answers (5)

bipen
bipen

Reputation: 36551

$(document).ready(function(){ is called whn the doucument is ready. and your addClass .. use single or double quotes to enclose the class ( not ‘ )..

try this...

 $(document).ready(function(){ 
   $('#sky').addClass('animate-in'); 
});

UPDATED

another div

<div id="anotherdiv"></div>

call the click function and the jquery selector to which u want to change the class

$('#anotherdiv').click(function(){
    $('#sky').addClass('animate-in');  //or any other new class
});    

go thorugh the selector jquery documentation..

http://api.jquery.com/category/selectors/

Upvotes: 2

Karthika Subramanian
Karthika Subramanian

Reputation: 346

Please check that already any classes are available in $("#sky") and if u want to use only 'animate-in',

Can try this,

$("#sky").attr('class','');

$("#sky").addClass('animate-in');

so that we can come to know that, already existing things are cleared and adding a new one

Upvotes: 0

Manish Nagar
Manish Nagar

Reputation: 1046

use this code

   <body onload='$("sky").addClass("animate-in")' >

Upvotes: 0

rahul
rahul

Reputation: 7663

you can do it like this

$(function () {
   $('#sky').addClass('animate-in'); 
});

OR

$(function () {
    $('#sky').addClass("animate-in"); 
});

Upvotes: 0

Adil
Adil

Reputation: 148180

You need to use single or double quote to enclose your class.

Change

$('#sky').addClass(‘animate-in’); 

To

$('#sky').addClass('animate-in'); 

or

$('#sky').addClass("animate-in"); 

Upvotes: 1

Related Questions