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

100
Views
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 answers
Answer question

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 Report

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 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!