LWC 3.8 / JS - how to toggle a (hidden) layer on and off?

Hello,
for a small adjustment of a JS code which was working fine under LWC 3.6 i would appreciate some help. I simply want to toggle a layer on and off - it works when the layer is visible in the legend with:
function toggleLayer() {
$(‘#node-LayerXYZ’).click();
}

The layer i want to toggle on and off should be hidden from the user, therefore i set it in the LM plugin to “hidden” - which makes it obviously impossible to simulate a “click” event. How could i switch a layer on and off?

Can you try something like :

let layer = lizMap.mainLizmap.state.rootMapGroup.getMapLayerByName('lampadaires');
layer.checked = true;
layer.checked = false;

Look at the demo https://demo.lizmap.com/lizmap/index.php/view/map?repository=javascript&project=lampadaires even if the script is designed for all LWC versions.

1 Like

This also only works when the layer is available in the legend - i have the layer set to be not displayed in the layer tree via LWC QGIS plugin: