Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
i don't know why this javascript code did not working, get text color problem in javascript

// (main.js)

const themechange = document.querySelector('button');
// const text = document.getElementById('text');

themechange.addEventListener('click', () => {
  if (document.getElementById('text').style.color == 'black') {
    document.getElementById('text').style.color = 'red';
  } else if (document.getElementById('text').style.color == 'red') {
    document.getElementById('text').style.color = 'blue';
  }
})
<head>
  <script src="main.js" defer></script>
</head>

<body class="chapter-3">
  <h2 id="title"></h2>
  <article id="mission">
    <p></p>
    <p>(#ff0000) -> (#0000ff) -> (#ff0000) -> (#0000ff) </p>
  </article>

  <div id="container">
    <button></button>
    <div id="text"></div>
  </div>
</body>

I don't know why this code did not working. similar code is working, but I think I missed code or idea of javascript. but I can't find..help

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're original text does not contains a defined color. So both if and else if are ignored.

Here a working example by defining the color first.

const themechange = document.querySelector('button');
const text = document.getElementById('text');
text.style.color = 'black'

themechange.addEventListener('click', () => {
  if (document.getElementById('text').style.color == 'black') {
    document.getElementById('text').style.color = 'red';
  }
  else if (document.getElementById('text').style.color == 'red') {
    document.getElementById('text').style.color = 'blue';
  }

})
<head>
  <script src="main.js" defer></script>
</head>

<body class="chapter-3">
  <h2 id="title">토글을 구현해보자</h2>
  <article id="mission">
    <p>JS를 이용해서 색변경 버튼을 누를때마다 버튼 아래에 있는 "안녕" 텍스트의 색이 빨강(#ff0000)와 파랑(#0000ff)로 번갈아가면서 변하도록 만들어주세요</p>
    <p>빨강(#ff0000) -> 파랑(#0000ff) -> 빨강(#ff0000) -> 파랑(#0000ff) 의 순서가 되게 해주세요</p>
  </article>

  <div id="container">
    <button>색변경</button>
    <div id="text">안녕</div>
  </div>
</body>

</html>

On the other hand, you may want to define a default behavior.

For example, the color start with black, then becomes blue but there is no case after that.

about 4 years ago · Juan Pablo Isaza Report

0

As the text color is not set initially you could simply assume that the original color is black. Changing your code to the following changes the initial (unset) color also to red first, on next button click the text-color is updated to blue:

// (main.js)

const themechange = document.querySelector('button');
// const text = document.getElementById('text');

themechange.addEventListener('click', () => {
    let textColor = document.getElementById('text').style.color;
    console.log({textColor}); //initially gives ''!
  if (textColor == 'black' || textColor == '') {
    document.getElementById('text').style.color = 'red';
  } else if (textColor == 'red') {
    document.getElementById('text').style.color = 'blue';
  }
});

Codepen

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!