172

Im building a draggable map that when the map is dragged the element is given a 'left' and 'top' attribute with values for each as so...

<div class="map" style="top:200px; left:100px;">Map</div>

I have a button that I want to remove the top and left attribute from the inline style on the div when clicked, Is this possible with jquery?

5
  • 9
    Yes it is. At least you can set them to an empty value, which removes it. Commented Feb 22, 2012 at 16:31
  • Possible duplicate: Remove CSS attribute using Jquery. The answers there may be helpful to you. Commented Feb 22, 2012 at 16:40
  • Note that ' ' doesn't count as an empty value only '' Commented Jul 19, 2013 at 13:21
  • 3
    To remove only one css property at a time: var cssObject = $('selector').prop('style'); cssObject.removeProperty('top'); cssObject.removeProperty('left'); Commented Oct 6, 2013 at 20:05
  • Just for the record, setting to empty does not always work: "Setting the value of a style property to an empty string... It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or <style> element." - see stackoverflow.com/questions/27791484/… Commented Jan 6, 2015 at 3:54

13 Answers 13

429

If you want to specifically remove top and left attributes and leave others, you can do this:

$('.map').css('top', '').css('left', '');

Or, a shorter equivalent:

$('.map').css({
    'top': '',
    'left': ''
});
Sign up to request clarification or add additional context in comments.

6 Comments

this is a better answer than the one selected.
Yep. This is the rightful answer. Didn't make sense at first, because it looks like ".css('top', '')" simply creates an empty 'top' rule, but no, it removes it completely.
This is the best answer yet. But I think I found another one. I tried a few different ones. .css('left', undefined); did not change anything. Passing null didn't do anything either. But .css('left', false); removes that property.
As others have suggested, this should be considered the correct answers.
@Viktor Note that the documentation explicitly says to use empty string. I wonder if false happens to work because of some undocumented type coercion that could go away in the future? api.jquery.com/css
|
140

The default values for CSS top and left are auto, so setting them to that might be equivalent depending on what you're trying to do:

$('.map').css('top', 'auto').css('left', 'auto');

You also have the option of wholly removing the style attribute:

$('.map').removeAttr('style');

However, if you're using other jQuery UI components, those may require inline styles that you don't want to be removed, so proceed with caution there.

6 Comments

usually such overwrites added to style attribute are meant for changing the value off from default css value; setting it to auto doesn't reset them to default value. meaning auto is not really same then not having the value at all. so correct answer would be $('map').css('top', '');
This answer is wrong, you're not removing attributes but replacing them with something else. Also, removing the whole style tag doesn't make much sense, one might want to keep some attrs and remove others.. wtrevino's answer is the correct one.
Its not an essential option so having it there is great! :) For fields like username
For the record, @wtrevino's answer should be the accepted one here. This particular solution worked for me under some circumstances, but I would remove this answer if I could. Unfortunately, since it's accepted, it won't let me.
This is not the correct answer... @wtrevino answer is the most correct. This answer will only work if you want to remove all static styles from the element... in most cases this is not what you want to do.
|
38

You can remove all of the contents in the style attribute by doing:

$('.map').removeAttr('style');

And you can remove specific styles by doing:

$('.map').css('top', '');
$('.map').css('left', '');

Comments

30

Simply set the CSS property with an empty string, for example with the following code:

$('#mydiv').css('color', '');

See jQuery Documentation on CSS.

1 Comment

Not sure why this isn't the accepted answer. The actual accepted answer is hacky because it doesn't actually remove the CSS property from the object, which is the title of the question.
6
  1. Go here: jQuery API
  2. Ctrl + F for 'remove'
  3. Read:

Setting the value of a style property to an empty string — e.g. $( "#mydiv" ).css( "color", "" ) — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property.

The docs give you the current, recommended approach for what you're trying to accomplish, which can often save you time because you don't have to read back and forth flame wars on stack overflow (no matter how fun/enlightening that may be!).

Comments

5

Per this JQuery bug report

element.removeAttr('style')
doesn't work consistently in Webkit based browsers. For example, I ran across this problem on iOS 6.1. The fix is to use:
element.attr('style', '')

Comments

2

If you want to remove all of it you can do

$('.map').removeAttr('style');

Comments

2
$.fn.removeCss=function(prop){
   if(!prop){
          return $(this).removeAttr('style').removeAttr('class');
     }
    else{
         return $(this).css(prop,null);
    }

}

then if you want to remove css prop(s) :

    $('#mydiv').removeCss('color');
//To remove all prop Css
    $('#mydiv').removeCss();

Comments

1

In my opinion the cleanest way is to remove the property completely from the element's CSSStyleDeclaration, instead of just overwriting it with some kind of null/zero/default value:

$(".foo").prop("style").removeProperty("top");
$(".foo").prop("style").removeProperty("left");
$(".foo").prop("style").removeProperty("background-color");

Comments

0
$.fn.removeCss=function(toDelete){

    var props=$(this).attr('style').split(';');
var tmp=-1;
for( var p=0;p<props.length; p++){if(props[p].indexOf(toDelete)!==-1){tmp=p}};
if(tmp!==-1){

   delete props[tmp];
}

  return $(this).attr('style',props.join(';')+';');

}

Delete safely with this plugin!

$('myDiv').removeCss('color');

Comments

0

There are some styles that can't be easily remove.(overflow comes to mind)

A workaround would be to create 2 different classes and add/remove the one containing the style you want.

NOT the best solution for style properties that can be easily remove/disabled.

Comments

0

To remove css styles assign an empty string to the style

in this case

$('.map').css({top:'',left:''});

Comments

-1
 $("#map").css("top", "0"); 
 $("#map").css("left", "0"); 

1 Comment

this sets it to 0, does not remove the property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.