Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
How do I change color of text in client side javascript?

I am adding a list item to my ul from an input textarea. How do I change it's color based on if it's a palindrome or not? Below is the js file of my code.

function isPalindrome(text){
    return text == text.split('').reverse().join('');
}

const staticForm = document.getElementById('static-form');
let myUl = document.getElementById('list');
const textarea = document.getElementById('text');
staticForm.addEventListener('submit', (event) => {
    event.preventDefault();
    if (isPalindrome(text)==true) textarea.style.color='red';
    else textarea.style.color='blue';
    let li = document.createElement('li');
    li.innerHTML = textarea.value;
    myUl.appendChild(li);
    myForm.reset();
    textarea.focus();
    const result = isPalindrome(textarea)
    
});

HTML:

<main>
        <form id='static-form' method="POST" class="new-post-form">
            <textarea id='text' name='phrase'>
                Attempt here
            </textarea>
        
            <input class="sub-button" type="submit" name="submit" value="Submit" /> 
        </form>
        <ul id='list'></ul>
        
</main>

I know that writing textarea.style.color does not work, what is the best way to achieve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

function isPalindrome(text) {
        return text === text.split("").reverse().join("");
 }

 const staticForm = document.getElementById("static-form");
 let myUl = document.getElementById("list");
 const textarea = document.getElementById("text");

 const submitBtn = document.getElementById("submit");
 submitBtn.addEventListener("click", (event) => {
        const text = document.getElementById("text").value;
        textarea.style.color = isPalindrome(text) ? "red" : "blue";
        let li = document.createElement("li");
        li.innerHTML = textarea.value;
        myUl.appendChild(li);
        textarea.focus();
        const result = isPalindrome(textarea);
      });
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
  </head>
  <body>
    <main>
      <form id="static-form" method="POST" class="new-post-form">
        <textarea id="text" name="phrase">sample text</textarea
        >

        <input
          class="sub-button"
          id="submit"
          type="button"
          name="submit"
          value="Submit"
        />
      </form>
      <ul id="list"></ul>
    </main>

 
  </body>
</html>

function changeTextareaColor(){
      const textarea = document.getElementById("textarea");
      if (textarea) {
        textarea.style.color = "red";
      }
}
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
  </head>
  <body>
    <textarea id="textarea">
      This is a static template, there is no bundler or bundling involved!
    </textarea>


    <button onclick="changeTextareaColor()">change color</button>
    <script>
      
    </script>
  </body>
</html>

I modify your code a little bit. I hope It will work.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda