Search code examples
javascriptjqueryzepto

removeAttr() not applied?


I'm using zepto.js for my current project which has the same removeAttr() method as jquery has.

i'm applying a margin-top to a bunch of elements – works fine.

var $apply = $('aside[role="sub"], aside[role="event-info"], aside[role="attend"]');
$apply.css('margin-top', '100px'); //works fine

However I also need to remove it again in a resize-event.

if ( $(window).width() <= 984 ) {
    //$apply.removeAttr('style'); //doesn't take effect
    $apply.css('margin-top', '0'); //works fine
    console.log('< 984');       
} 

So i'm able to set the margin-top back to 0 but can't completely remove the style attribute from the selector.

Any ideas why? I don't get any erros, it just doesn't take effect.


Solution

  • The correct way to remove it, is by not setting a value when using the .css.

    $apply.css('margin-top', ''); // remove property
    

    When a value for a property is empty, that property is removed.

    Read more here.