<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Getting name of activated layer of LayerList in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522751#M85389</link>
    <description>&lt;P&gt;Thank you very much, that helped me a lot &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;One more solution to have a look to special layers is:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        reactiveUtils.watch(() =&amp;gt; layer.visible, (visible) =&amp;gt; {
          alert ("hello: "+layer.visible);
});&lt;/LI-CODE&gt;</description>
    <pubDate>Fri, 16 Aug 2024 08:16:19 GMT</pubDate>
    <dc:creator>KaiBehncke</dc:creator>
    <dc:date>2024-08-16T08:16:19Z</dc:date>
    <item>
      <title>Getting name of activated layer of LayerList</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522109#M85366</link>
      <description>&lt;P&gt;Dear users,&lt;/P&gt;&lt;P&gt;in my application I use the LayerList Widget and an Expand-Div to place "hard-coded" legend-images:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt; const layerList = new LayerList({
            view: view
          });
          
          
          
          const expandbutton = new Expand({
  view: view,
  content: "Legende&amp;lt;br&amp;gt;(aktuell kein Layer aktiviert)",
  label: "Legende",
  id: "legendendings",
  expandTooltip:"Legende",
  expanded:false,
  container: document.createElement("div")
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;E.G. if the users has actived the layer "climate" a hard-coded image like "climatelegend.png" has to be placed in the legend-container.&lt;/P&gt;&lt;P&gt;I wonder now: How can I&amp;nbsp; find out which layer of the LayerList is activated?&lt;/P&gt;&lt;P&gt;If I open the legend-Expand I would like to have that information.&lt;/P&gt;&lt;P&gt;I tried things like:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;reactiveUtils.watch(
    () =&amp;gt; [expandbutton.expanded], ([expandbutton]) =&amp;gt; {
      alert ("hello: "+layerList.selectedItems.layer);
    });&lt;/LI-CODE&gt;&lt;P&gt;...but so far I don`t get the layer-name?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 09:44:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522109#M85366</guid>
      <dc:creator>KaiBehncke</dc:creator>
      <dc:date>2024-08-15T09:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: Getting name of activated layer of LayerList</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522146#M85367</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/724978"&gt;@KaiBehncke&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-LayerList.html#selectedItems" target="_self"&gt;selectedItems&lt;/A&gt; is a collection so you need to grab the first list item in the collection, then get the layer.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;alert ("hello: "+layerList.selectedItems[0].layer.title);&lt;/LI-CODE&gt;&lt;P&gt;Make sure you have &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-LayerList.html#selectionMode" target="_self"&gt;selectionMode&lt;/A&gt; set to "single".&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 11:50:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522146#M85367</guid>
      <dc:creator>Sage_Wall</dc:creator>
      <dc:date>2024-08-15T11:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Getting name of activated layer of LayerList</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522217#M85372</link>
      <description>&lt;P&gt;Hi Sage_Walls, thank you for answering.&lt;/P&gt;&lt;P&gt;Maybe I`m totally "on the wrong path".&lt;BR /&gt;I don`t get it to run the way you described.&lt;/P&gt;&lt;P&gt;I try now with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const layerList = new LayerList({
            view: view,
            selectionMode: "single"
        });
    
layerList.selectedItems.on("change", ({ added, removed }) =&amp;gt; {
alert ("hello");
alert (layerList.selectedItems[0].layer.title);
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...but I get "Uncaught TypeError: layerList.selectedItems[0] is undefined" (you see it in the screenshot).&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;A thing I don`t understand at all in generally is:&lt;/P&gt;&lt;P&gt;I would like to get a reaction (e.g. a "hello") if I click on the eye-symbol of the layer ("area 2" of the image). But nothing at all happens.&lt;BR /&gt;I get a "hello", if I click in "area 1" (directly on the layer-name).&lt;/P&gt;&lt;P&gt;I wonder: What shall I do (with what function) to get a reaction if I click on the eye-symbol?&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="undefined.png" style="width: 979px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/112455i707F99BAFC9E56BA/image-size/large?v=v2&amp;amp;px=999" role="button" title="undefined.png" alt="undefined.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="selection.png" style="width: 456px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/112456iAC1D1E3957612E87/image-size/large?v=v2&amp;amp;px=999" role="button" title="selection.png" alt="selection.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 14:22:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522217#M85372</guid>
      <dc:creator>KaiBehncke</dc:creator>
      <dc:date>2024-08-15T14:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: Getting name of activated layer of LayerList</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522424#M85377</link>
      <description>&lt;P&gt;Happy to help&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/724978"&gt;@KaiBehncke&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;Your not alone it's a common question.&amp;nbsp; An item in the layer list can be selected (area 1 in your screenshot) or the visibility on the layer can be changed with the eyeball (area 2). The selection allows for things like showing a layer's properties in a different panel without affecting the visibility of the layer.&amp;nbsp; In your case, if you want to respond to a change in the visibility of the layer you have a couple options.&amp;nbsp; One of which is to create a &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-LayerList.html#listItemCreatedFunction" target="_self"&gt;listItemCreatedFunction&lt;/A&gt; and set up the watch on an individual layer's visible property.&amp;nbsp; Another way would be to watch and respond to changes directly on the layers collection, which might be better if you wanted to show the legends for all the visible layers in the map.&amp;nbsp; I created a codepen to hopefully show how this can be done.&lt;/P&gt;&lt;P&gt;&lt;A href="https://codepen.io/sagewall/pen/ZEdvLmy" target="_blank"&gt;https://codepen.io/sagewall/pen/ZEdvLmy&lt;/A&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Watch the selected items array for the first selected item and log it's title
    reactiveUtils.watch(
      () =&amp;gt; layerList.selectedItems?.getItemAt(0),
      (selectedItem) =&amp;gt; {
        console.log("Title from selectedItems:", selectedItem?.layer.title);
      }
    );

    // Create a listItemCreatedFunction and watch the list items layer.visible property
    function listItemCreatedFunction(event) {
      const { item } = event;
      const { layer } = item;

      reactiveUtils.watch(
        () =&amp;gt; layer.visible,
        () =&amp;gt; {
          if (layer.visible) {
            console.log(layer.title, " is visible");
          } else {
            console.log(layer.title, " is not visible");
          }
        },
        { initial: true }
      );
    }
  });

  // Watch for all the visible layers in the map and respond when one changes
  reactiveUtils.watch(
    () =&amp;gt;
      view.map.layers
        .filter((layer) =&amp;gt; layer.visible)
        .map((layer) =&amp;gt; layer.title)
        .reverse(),
    (titles) =&amp;gt; {
      titles.forEach((title) =&amp;gt; {
        console.log(
          "From the watch on view.map.layers: ",
          title,
          " is visible"
        );
      });
    }
  );&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 18:07:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522424#M85377</guid>
      <dc:creator>Sage_Wall</dc:creator>
      <dc:date>2024-08-15T18:07:01Z</dc:date>
    </item>
    <item>
      <title>Re: Getting name of activated layer of LayerList</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522751#M85389</link>
      <description>&lt;P&gt;Thank you very much, that helped me a lot &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;One more solution to have a look to special layers is:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        reactiveUtils.watch(() =&amp;gt; layer.visible, (visible) =&amp;gt; {
          alert ("hello: "+layer.visible);
});&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 16 Aug 2024 08:16:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522751#M85389</guid>
      <dc:creator>KaiBehncke</dc:creator>
      <dc:date>2024-08-16T08:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: Getting name of activated layer of LayerList</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522858#M85393</link>
      <description>&lt;P&gt;Somebody might have the same problem.&lt;BR /&gt;With the following solution it works (with predefined images for the legend):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const expandbutton = new Expand({
  view: view,
  content: "Legende&amp;lt;br&amp;gt;",
  label: "Legende",
  id: "legendendings",
  expandTooltip:"Legende",
  expanded:false,
  container: document.createElement("div")
});

view.ui.add(expandbutton, "bottom-right");


function listItemCreatedFunction(event) {
      const { item } = event;
      const { layer } = item;

      reactiveUtils.watch(
        () =&amp;gt; layer.visible,
        () =&amp;gt; {
               var mylittlelegende = document.getElementById('legendendings_controls_content');
          if (layer.visible) {
            
 
           var pp = document.createElement("span");
           pp.setAttribute("id", layer.title);
            mylittlelegende.appendChild(pp);
            var pfadname=layer.title+"legende.png";
           
            var image = document.createElement("img");
            image.src=pfadname; 
            pp.appendChild(image);

          } else {
            var pp = document.getElementById(layer.title);
            mylittlelegende.removeChild(pp);
          }
        }
      );
    };
    
          const layerList = new LayerList({
            view: view,
            selectionMode: "single",
            listItemCreatedFunction:listItemCreatedFunction
        });
        
         view.ui.add(layerList, "bottom-left");&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 16 Aug 2024 13:30:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-name-of-activated-layer-of-layerlist/m-p/1522858#M85393</guid>
      <dc:creator>KaiBehncke</dc:creator>
      <dc:date>2024-08-16T13:30:29Z</dc:date>
    </item>
  </channel>
</rss>

