2

It's really hard for me to figure this out. But can anyone give me a sample script where I can just add !important to an already existing inline css?

pau
  • 235
  • 1
  • 2
  • 9

6 Answers6

3

You can add !important to inline using below code,

Suppose you want to add inline css for div tag then you can do that using below code.

CSS way

<div style="color:red !important"

jQuery

$('div').attr('style', 'color:red !important;');
Dipesh Parmar
  • 26,601
  • 7
  • 57
  • 86
2

You can get the inline styles using the attr, and manipulate them like below,

var styleCode = $('p').attr('style').split(";");
$('p').attr('style',styleCode.join(' !important;'));

Check this Fiddle

RevanthKrishnaKumar V.
  • 1,827
  • 1
  • 20
  • 33
Ashis Kumar
  • 6,364
  • 2
  • 19
  • 35
1

.attr()

$(element).attr('style', 'height:200px !important;')
Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107
1

HTML

<div class="yourelementclass"></div>  

JS Code

  $('.yourelementclass').attr('style', 'width:500px !important;')

Here is jsfiddle

Love Trivedi
  • 3,739
  • 3
  • 18
  • 26
1

A non-library specific way would be to use the += operator to append !important to the value.

<div style="width:50px;" id="id1"></div>

document.getElementById("id1").style.width += " !important";

James Bruckner
  • 879
  • 7
  • 10
0
$('img')[0].style.setProperty( 'width', '500px', 'important' );
Igor
  • 31,955
  • 14
  • 75
  • 111
Max Hartshorn
  • 709
  • 7
  • 19