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

196
Visualizações
Javascript and html to show or hide an element

Hi I think this is very easy but I just can't seem to twig it at the moment. I want to use a javascript function to set the visibility of an html tag. I realise the below is wrong as hidden doesn't take a boolean. I'm just struggling to click what the easiest way to do it is ?

So I have some script like this

<script>
function evaluateBoolean() {
    if (location.hostname.indexOf("someval" > 0) {
        return true;
    } else {
      return false;
    }
</script>

and I wanted to use iut something like this

<div hidden="evaluateBoolean()"> this will be shown or displayed depending on the javascript boolean </div>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can't explicitly run js in your html, if you aren't using any framework like angular or react, where property binding is allowed.
For achieving your intentions with js you can use this approch:

  1. Add to your div an id:

     <div id="myDiv"> Toggled div </div>
    
  2. In your js script modify your function evaluateBoleean() to show/hide the element:

    function evaluateBoolean() {
    
    const div = document.querySelector("#myDiv");
    
    if (location.hostname.indexOf("someval" > 0) {
    
        div.hidden = true;
    
    } else {
    
        div.hidden = false;
    }
    
about 4 years ago · Juan Pablo Isaza Relatório

0

There's a very easy option:--> having a blank text firsly replace the html code with this:-->

<div hidden="evaluateBoolean()" id="ThingToBeHidden"> this will be shown or displayed depending on the javascript boolean </div>

and put js code:-->

document.getElementById("ThingToBeHidden").innerHTML = "";

So you have assigned the div to have it's special id which none other element has. So now the js code selects the div with that id and then sets the context of it to blank. If you want the text to appear again, the js code is:-->

document.getElementById("ThingToBeHidden").innerHTML = "this will be shown or displayed depending on the javascript boolean";
about 4 years ago · Juan Pablo Isaza Relatório

0

I would recommend doing it by altering the display style in the JavaScript code.

const el = document.getElementById('container');
    const btn = document.getElementById('btn');

    btn.addEventListener('click', function handleClick() {
    if (el.style.display === 'none') {
        el.style.display = 'block';

        btn.textContent = 'Hide element';
    } else {
        el.style.display = 'none';

        btn.textContent = 'Show element';
    }
    });
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