drpcken drpcken - 3 months ago 14
jQuery Question

Adding and removing style attribute from div with jquery

I've inherited a project I'm working on and I'm updating some jquery animations (very little practice with jquery).

I have a div I need to add and remove the style attribute from. Here is the div:

<div id='voltaic_holder'>


At one point in the animation I need to add a style to it:

<div id='voltaic_holder' style='position:absolute;top:-75px'>


I've searched around and found the
.removeAttr()
method but I can't see how to add it, or even remote parts of it (like the top:-75px only).

Thanks,

Answer

You could do any of the following

Set each style property individually:

$("#voltaic_holder").css("position", "relative");

Set multiple style properties at once:

$("#voltaic_holder").css({"position":"relative", "top":"-75px"});

Remove a specific style:

$("#voltaic_holder").css({"top": ""});
// or
$("#voltaic_holder").css("top", "");

Remove the entire style attribute:

$("#voltaic_holder").removeAttr("style")
Comments