Unable to override height with! Important with javascript

I have div.innerDivone that has been assigned height: 300px !important;in the CSS file. I can neither modify the existing CSS file nor add a new class to override. Since the style !important, so you can override it as a style inline, only !important. But this does not give the desired effect.

Submit a demo here .

UPDATE: I needed only the inline style.

HTML:

<div>
    <div class="innerDiv">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
        software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

CSS

.innerDiv {
  height: 300px !important;
  width: 150px;
  border: 1px solid;
}

JS:

function overrideHeight() {
    document.getElementsByClassName('innerDiv')[0].style.height = 400 + 'px !important';
}

overrideHeight();
+4
source share
4 answers

In my browser (iPad Safari), Matt's answer only works if it's modified:

function overrideHeight() {
document.getElementsByClassName('innerDiv')[0].style.cssText = "height: 400px !important";
}

- .

+1

:

function overrideHeight() {
  document.getElementsByClassName('innerDiv')[0].style = "height: 400px !important";
}

. .

, !important , .. style.height. style , .

, . , , , - .

+4

Matt , , JS <style> :

var styleTags = document.createElement('style');
styleTags.type = "text/css";
var styleText = document.createTextNode('.innerDiv { height: 400px !important; } ');
styleTags.appendChild(styleText);
document.getElementsByTagName('head')[0].appendChild(styleTags);
.innerDiv {
  height: 300px !important;
  width: 150px;
  border: 1px solid;
}
<div>
    <div class="innerDiv">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
        software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

.

+1
source

Another alternative:

document.getElementsByClassName ('innerDiv') [0] .style.setProperty ("height", "400px", "important");

0
source

Source: https://habr.com/ru/post/1630043/


All Articles