136

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".

Syscall
  • 19,327
  • 10
  • 37
  • 52
Kreker
  • 2,438
  • 3
  • 23
  • 27
  • If it is `none` why do you need to remove it? If you need to change it to another value [here](https://developer.mozilla.org/en/CSS/-moz-user-select) is a list of valid values – Chad May 10 '11 at 14:14

3 Answers3

271

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", "");
Syscall
  • 19,327
  • 10
  • 37
  • 52
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
0

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

vsync
  • 118,978
  • 58
  • 307
  • 400
-3

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.

dmorlock
  • 1,993
  • 4
  • 18
  • 22