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

78
Views
Show / hide layers from data attribute

I'm trying to write a function that let's me hide / show leaflet layers. The data on which layer is to be shown / hidden is fetched from a data attribute.

Obviously, as the latter is a string, leaflet will throw errors (leaflet.js:5 Uncaught TypeError: Cannot create property '_leaflet_id' on string 'someLayer')

Here's my code:

<div class="single-filter" data-layer="someLayer">

let someLayer = L.layerGroup([
  L.marker([2131.25, 2854.25], { icon: someIcon })
]).addTo(map);


$('.single-filter').on('click', setFilter);

function setFilter() {
  let layer = $(this).data('layer');
  toggleLayer(layer);
}

function toggleLayer(layer) {
  if (map.hasLayer(layer)) {
    map.removeLayer(layer);
  } else {
    map.addLayer(layer);
  }
}

How would I be editing this so it actually recieves the right data format? (e.g. the object someLayer instead of the string someLayer).

Thanks for your assistance!

about 4 years ago · Juan Pablo Isaza
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!