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

101
Visualizações
Showing html elements only if available

So I'm trying to fill popups of a Leaflet JS Map with Content from a csv table. The popup is filled with html. It perfectly fits my needs. The only problem I have is that not every popup has a image available, but every popup depends on the same html code shown below:

popUp = "<h2>"+feature.properties.Name+"</h2>" +
        "<img src='"+ feature.properties.picturelink +"'width='300'</img>" +"<br>"+"<br>"+
        "<h4>"+feature.properties.description+"<br>"

If there's no image available, a place holder is shown: placeholder

Is there a way to only show actual images and to hide the placeholder if there is none?

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

0

You can add onerror to the image to hide it if broken.

onerror="this.style.display='none'"
about 4 years ago · Juan Pablo Isaza Relatório

0

I'd suggest:

// here we're using a template literal which allows us to interpolate
// JavaScript variables (wrapped in the ${...}) directly into the string
// which avoids having to concatenate strings; this also allows new-lines
// within the string itself:
let popUp = `<h2>${feature.properties.Name}</h2>
             <img src="${feature.properties.picturelink}"
                  width="300"
                  // here we use a conditional/ternary operator,
                  // if the feature.properties.picturelink property
                  // is falsey then the style of the <img> is set
                  // to display: none; otherwise - if the property
                  // is truthy - it's set to display: auto:
                  style="${ feature.properties.picturelink ? 'auto' : 'none' }">
             <br><br>
             <h4>${feature.properties.description}</h4>`;

References:

  • Conditional (Ternary) operator.
  • Template literals.
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