2

On my page I want to set the attribute display to block!important when there's a specific string in the url,

I've been doing some research, but can't find the correct answer. Using the code below I can set the background-color: green!important on the body, but I don't know how to target something else (the "loginActive" id element).

:javascript
  if(window.location.href.indexOf("sign_up") > -1) {
    document.body.style.setProperty ("background-color", "green", "important");
    document.getElementById("loginActive").style.display = "block", "important";
  }

Any tips on how I can set display: block!important on the element #loginActive ?

4
  • 2
    You shouldn't need to use !important if you're setting styles on the element itself, unless there's something else that's !important elsewhere. If the one elsewhere is !important, presumably whoever added it decided it shouldn't be overridden. Generally, the use of !important is seen as a bad solution to a CSS issue that will come back to bite you, as it sounds it may have done here. Commented Jan 26, 2016 at 10:42
  • Can you use Jquery ? Commented Jan 26, 2016 at 10:42
  • I'm no fan of using !important, but I'm using it with a angular ng-show function. Commented Jan 26, 2016 at 10:52
  • document.getElementById("loginActive").style.display = "block", "important"; – that makes no sense, resp. it doesn’t do what you think it does. (Go look up the JS comma operator, if you want to know what your line actually means.) And I don’t understand the question – in the line above, you are able to set a style property with important for the body – so what is stopping you from using that exact same syntax in the second line as well …? Commented Jan 26, 2016 at 11:22

1 Answer 1

4

Try this

var item = document.getElementById("loginActive");
item.style. display = 'block !important';

Or

item.setAttribute('style', 'display:block !important');

But you shouldn't do that

Sign up to request clarification or add additional context in comments.

4 Comments

That's no different then document.getElementById("loginActive").style.display = "block", "important";
:javascript if(window.location.href.indexOf("sign_up") > -1) { var item = document.getElementById("loginActive"); item.style.setProperty ("display", "block", "important"); } changed the code a bit, workse now.
My bad, setAttribute actually works better, but you should consider using a class with display: !important and append with to the element
@alucardu: Of course that is significantly different – mainly because in your code, you are not setting important at all (you just have a string literal “on its own”, using the comma operator after the first assignment.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.