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

181
Visualizações
How can I use JavaScript to print non-visible code?

I am currently using the following script to echo visible text on the screen...

<h1 id="MyText"></h1>

<script>
var el = document.getElementById('MyText');
{ if (italian || french || german)
{ el.innerHTML = (
(italian && 'ciao') ||
(french && 'salut') ||
(german && 'hallo') ); }
else { el.innerHTML = 'hello';}
}
</script>

How should I edit this script, to make it work in case I want it to type a non visible word in the html?

Example:

  • In CSS I have:
#ciao{height:350px;}
#salut{height:10px;}
#hallo{height:3000px;}
  • In my code I want to define my table height basing on these conditions:
<div id="MyText">

The problem is with the above code, this will result in printing a visible text, instead of rather choosing the css id. So how am I supposed to edit the script?

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

0

It is more idiomatic to add an appropriate class to the div element. [edit] it appears as though you are looking to set the class based on the contents of the div. This is generally a difficult thing to do but here is one approach. Another would be to set a property of the div and then do a :before hack to create a pseudo element with the text of the property.

const langauges = {
  "ciao":"italian",
  "salut":"french",
  "hallo":"german",
  "hello":"english"
}

const updateClass = (el)=>{
  if(langauges[el.innerHTML] != undefined){

    el.className = langauges[el.innerHTML];
  }
}

const onChange = (event)=>{
  const el = event.target;
  updateClass(el);
}

var el = document.getElementById('MyText');
el.addEventListener("input",onChange,false);
updateClass(el);
.italian{height:350px; color:blue;}
.french{height:10px; color:red;}
.german{height:3000px; color:yellow;}
.english{height:3000px; color:purple;}
<h1 id="MyText" contenteditable="true">ciao</h1>

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