Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove only position(top and left) of Style attr using JQuery?

I am dynamically setting the position attr of the element only when the view is out of viewport of the window, in other case default value is set from the css file ,

.css( { "left": (left + 20) + "px", "top": (top+10) + "px" } );

Once the dynamic position is set, I want to remove the position attr alone.

I can remove style attribute it will also my display property of style which is required.

Is there a way to to remove the position attr alone?

like image 408
ravikiran Avatar asked Sep 29 '09 05:09

ravikiran


People also ask

How do I remove a specific attribute in jQuery?

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 do I remove a style from a specific element?

removeProperty() method is used to remove a property from a style of an element. The style of the element is selected by going through the styleSheets array and selecting the cssRule. The removeProperty method can then be specified with the property to be removed.

Which of the following jQuery function gets the top and left position values?

jQuery position() Method This method returns an object with 2 properties; the top and left positions in pixels.

What jQuery method is used to completely remove attributes from elements?

The removeAttr() method removes one or more attributes from the selected elements.


2 Answers

Perhaps, your best Bet would be to simply put them to their default value. Top and left have the default value "auto". So:

jQuery(selector).css({
   'top': 'auto',
   'left': 'auto'
})
like image 57
Mike Avatar answered Oct 20 '22 11:10

Mike


$(el).css('top', ''); will remove the inline style declaration, reverting back to either what is specified in your stylesheet or the default.

like image 44
Kevin C. Avatar answered Oct 20 '22 13:10

Kevin C.