Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove CSS attribute using Jquery

Tags:

jquery

css

All of the other answers I have discovered only remove the setting of the attribute, and not the attribute completely. I am changing an element from absolute to fixed positioning. I need to remove the right positioning attribute and replace it with margin-right so that the element is position right within its parent DIV. If the right attribute is not removed, the element goes all the way to the right of the screen, and not to the right of the DIV like I need it to. Can anyone offer a suggestion on how to accomplish this?

like image 203
user981053 Avatar asked Nov 03 '11 05:11

user981053


People also ask

How to remove attribute from a jQuery object?

The.css () jQuery method is used to set or return one or more style properties for the selected elements. The.removeAttr () jQuery method removes an attribute from each element in the collection of matched elements.The method uses the JavaScript removeAttribute () function, but it is capable of being called directly on a jQuery object.

How to remove CSS “top” and “left” attribute with jQuery?

- GeeksforGeeks How to remove CSS “top” and “left” attribute with jQuery ? Method 1: Using the css () method: The css () method is used to get or set CSS properties of a selected element. It takes two arguments, the first argument is the property that has to be set and the second argument is the value that it has to be set to.

How to remove all style attribute from each tag using jQuery?

jQuery Web Development Front End Technology To remove an attribute from each tag using jQuery, use the removeAttr () method and use the Universal Selector. Let us see how to use the method to remove all style attribute. Use the universal selector also to select all the elements.

How to remove the style of an element using jQuery?

There is another way to remove style. Simply use jQuery removeAttr () method: Be careful because the following method removes all other properties in the style attribute. The .css () jQuery method is used to set or return one or more style properties for the selected elements.


3 Answers

Try setting it to its default value auto

$(element).css('right', 'auto');
like image 173
mrtsherman Avatar answered Sep 29 '22 22:09

mrtsherman


$('div').css({'right' : '', 'margin-right' : '100px'});

If that doesn't work, try setting right to it's default value of auto

like image 35
Derek Hunziker Avatar answered Sep 29 '22 22:09

Derek Hunziker


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("right");
$(".foo").prop("style").removeProperty("background-color");
like image 38
Martin Avatar answered Sep 29 '22 21:09

Martin