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?
5 Answers
Try setting it to its default value auto
$(element).css('right', 'auto');
4 Comments
mrtsherman
PS - the CSS spec could have answered this for you - w3.org/TR/CSS21/visuren.html#propdef-right
Phrogz
Better than an explicit hard-coded default value is an empty string (as demonstrated by @Derek) which lets other stylesheet rules apply as appropriate.
mrtsherman
@Phrogz - I assumed blanking the attribute was not working according to OP's question.
All of the other answers I have discovered only remove the setting of the attribute, and not the attribute completely.user981053
He is correct. I tried Derek's method prior to posting this question and it did not work.
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");
Comments
You can try following code
$.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) {
props.splice(tmp, 1);
}
return $(this).attr('style',props.join(';'));
}
example usage:
$(selector).removeCss('color');