How to remove only one style property with jQuery?

JqueryCss

Jquery Problem Overview


I have a div with this property style="-moz-user-select:none; position:static !important;". I need to remove the -moz-user-select Tried with $(selector).css() but I don't know what value to set because it's "none".

Jquery Solutions


Solution 1 - Jquery

The documentation for css() says that setting the style property to the empty string will remove that property if it does not reside in a stylesheet:

> 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. It does not, however, > remove a style that has been applied > with a CSS rule in a stylesheet or > <style> element.

Since your styles are inline, you can write:

$(selector).css("-moz-user-select", "");

Solution 2 - Jquery

I know this question is about jQuery, but want to add (for those like myself, who stumble here from Google) a solution with vanilla js:

(Based on my GIST)

Below is a breakdown of the method, but removing the comments and it's pretty much small and simple one-liner split->filter->join

const removeStyleProp = (elm, prop) => 
  elm.style.cssText = elm.style.cssText // "top: 0px; bottom: 50px; text-align: right;"
    .split('; ') // ["top: 0px", "bottom: 50px", "right: 2px", "text-align: right;"]
    .filter(p => !p.startsWith(prop) ) // ["top: 0px", "bottom: 50px", "text-align: right;"]
    .join(';');

// usage:
removeStyleProp(test, 'right')
console.log( test.style.cssText )

<div id='test' style='top:0;right:5px;bottom:50px;right:2px;text-align:right;'></div>

Alternately could also be done with Regex

Solution 3 - Jquery

You can also replace "-moz-user-select:none" with "-moz-user-select:inherit". This will inherit the style value from any parent style or from the default style if no parent style was defined.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKrekerView Question on Stackoverflow
Solution 1 - JqueryFrédéric HamidiView Answer on Stackoverflow
Solution 2 - JqueryvsyncView Answer on Stackoverflow
Solution 3 - JquerydmorlockView Answer on Stackoverflow