0

I need to create a button to change my li's to black.

html

 <h2>Sonnenfarben</h2>
  <div class="box">
     
    <ul type="circle" class="liste2">
      <li class="farbeRot">rot</li>
      <li class="farbeOrange">orange</li>
      <li class="farbeGelb">gelb</li>
    </ul>
    
    <button onclick="changeColor()">Lights out</button>

  <ol class="liste3" start="3">
      <li class="farbeBraun">braun</li>
      <li class="farbeGrau">grau</li>
  </ol>

css

.farbeRot{
  color: ;
  font-weight: bold;
}

.farbeOrange{
  color: orange;
  font-style: italic;
}

.farbeGelb{
  color: yellow;
}

.farbeBraun{
  color: brown;
  font-style: italic;
}

.farbeGrau{
  color: grey;
  font-weight: bold;
}

.box{
  color: black;
}

JS

function changeColor(){
  document.querySelector('.box').style.color = "black";
}

Tried to connect everything but didn´t work out.

edit: put in all colors I defined already. Tried deleting them etc. but it still did not work.

2
  • document.querySelector('.box').style.color = "black" is the same like .box{color: black;} which make it not show effect Commented Nov 17, 2022 at 9:33
  • Have in mind that there isn't a closing div tag for your box div element Commented Nov 17, 2022 at 9:37

2 Answers 2

2

You need to change the default color of .box from black to other values,such as red or green

.box{
  color: red;
}

function changeColor(){
  let lis = document.querySelectorAll('.liste2 > li,liste3 > li');
  for(let i=0;i<lis.length;i++){
    lis[i].style.color = "black";
  }
}
.farbeRot{
  color: green;
  font-weight: bold;
}

.farbeOrange{
  color: orange;
  font-style: italic;
}

.farbeGelb{
  color: yellow;
}

.farbeBraun{
  color: brown;
  font-style: italic;
}

.farbeGrau{
  color: grey;
  font-weight: bold;
}

.box{
  color: black;
}
<h2>Sonnenfarben</h2>
  <div class="box">
     
    <ul type="circle" class="liste2">
      <li class="farbeRot">rot</li>
      <li class="farbeOrange">orange</li>
      <li class="farbeGelb">gelb</li>
    </ul>
   

  <ol class="liste3" start="3">
      <li class="farbeBraun">braun</li>
      <li class="farbeGrau">grau</li>
  </ol>
  
   <button onclick="changeColor()">Lights out</button>
</div>

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

9 Comments

Stil doesn´t work. I defined colors for every li before already. Is that causing an error?
@Pekz Can you add full code so that we can analysis it
@Pekz if you override the color by setting a style to the list-items then you must set the color using the same selector and not the parent list element.
@lucumt edited the code in
@Pekz You need to follow Fabrizio Calderan's comment,that's the reason,you can check my updated answer now
|
0

if you just wanna change your li's color by a click event, don't do it that manually(almost). write a CSS-styled class for your ul and toggle it on click event

function changeColor() {
  const liste2 = document.querySelector(".liste2");
  liste2.classList.toggle("black-ul")
  
  /*
  if you just want the lights off and not toggle, replace above "toggle" with "add"
  simple javascript my boy
  */
  
}
.farbeRot{
  color: green;
  font-weight: bold;
}

.farbeOrange{
  color: orange;
  font-style: italic;
}

.farbeGelb{
  color: pink;
}

.farbeBraun{
  color: brown;
  font-style: italic;
}

.farbeGrau{
  color: grey;
  font-weight: bold;
}

.box{
  color: black;
}


/* below code is to make all the li black */

.black-ul > li {
  color: black
}
<div class="box">
     
    <ul type="circle" class="liste2">
      <li class="farbeRot">rot</li>
      <li class="farbeOrange">orange</li>
      <li class="farbeGelb">gelb</li>
    </ul>
    
    <button onclick="changeColor()">Toggle light</button>
</div>

3 Comments

A toggle button would even be better, thx. I copied everything but it sadly doesn´t work out
then the issue might be on any other part of your code. are you getting any errors on the console?
Solved everything thanks for your help!

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.