0

I am having difficulty to change background color using JavaSript. I only want to change color of background property that is inside .diagnosis-kit-inner .nav-tabs .nav-item.active:after. Can anyone help me please.Can anyone help me please? Thanks

function changeColor() {
  var changeColor = document.getElementsByClassName(".diagnosis-kit-inner .nav-tabs .nav-item.active:after");

  changeColor.style.backgroundColor = 'blue';
}
.diagnosis-kit-inner .nav-tabs .nav-item.active:after {
  background-color: white;
}
<div class="diagnosis-kit-inner ">
  <ul class="nav-tabs">
    <li class="nav-item">
    </li>
  </ul>
</div>
<a href="#" onclick="changeColor()">Change Color</a>

2
  • You have a typo here: onclick="changColor()" Commented Jul 9, 2020 at 19:44
  • Thanks for ur response. I have fixed this error. Can u please help me out to sort out this issue. Thanks Commented Jul 9, 2020 at 19:45

1 Answer 1

1

You cannot change the style of a psudo-selector, but you can use a var() for the background color and assign a default at the document :root.

You can then overwrite this using JavaScript by calling setProperty on the style of the documentElement.

This was adapted from this answer: Changing CSS pseudo-element styles via JavaScript

function changeActiveColor() {
  document.documentElement.style.setProperty('--nav-item-active-bg', 'blue');
}
:root {
  --nav-item-active-bg: white;
}

.diagnosis-kit-inner .nav-tabs .nav-item.active:after {
  color: #CCC;
  content: 'Active';
  padding: 0.25em;
  background-color: var(--nav-item-active-bg); /* Dynamic var prop */
}
<div class="diagnosis-kit-inner">
  <ul class="nav-tabs">
    <li class="nav-item active"></li>
  </ul>
</div>
<a href="#" onclick="changeActiveColor()">Change Color</a>

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

Comments

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.