7

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 5

13

Try setting it to its default value auto

$(element).css('right', 'auto');
Sign up to request clarification or add additional context in comments.

4 Comments

PS - the CSS spec could have answered this for you - w3.org/TR/CSS21/visuren.html#propdef-right
Better than an explicit hard-coded default value is an empty string (as demonstrated by @Derek) which lets other stylesheet rules apply as appropriate.
@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.
He is correct. I tried Derek's method prior to posting this question and it did not work.
2
$('div').css({'right' : '', 'margin-right' : '100px'});

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

Comments

2

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

1

Try setting right to 0px and then set the margin-right.

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

Comments

0

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');

Comments

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.