javascript - jQuery: how to loop through elements with data attribute -
I have several divs that look like this:
& lt; Div class = 'popupDiv' data-layergroup = 'layer 1' & gt; Dividend 1 & lt; / Div & gt; & Lt; Div class = 'popupDiv' data-layer group = 'layer 1' & gt; Divantent 2 & lt; / Div & gt; & Lt; Div class = 'popupDiv' data-layer group = 'layer 2' & gt; Divistant 3 & lt; / Div & gt; & Lt; Div class = 'popupDiv' data-layer group = 'layer 2' & gt; Development 4 & lt; / Div & gt; I am slightly stumped like a loop through some popsdave divis, and then loop through each different lagergram. I want to end with an array for each large group I wanted something like this:
var mainArray = []; $ ('.PopupDiv') each (function () {var tempArray = []; $ ([unique layer value]) each (function () keep the div value from layergroup in {// tempArray}); mainArray.push (TempArray );}); Return home; But I do not know the syntax I see. what I have to do?
You have the same id multiple code This is not acceptable in jQuery. Either remove it or make it unique or class & lt; Div class = 'popupDiv' data-layer = 'layer 1' & gt; & Lt; / Div & gt; & Lt; Div class = 'popupDiv' data-layer = 'layer 1' & gt; & Lt; / Div & gt; & Lt; Div class = 'popupDiv' data-layer = 'layer 2' & gt; & Lt; / Div & gt; & Lt; Div class = 'popupDiv' data-layer = 'layer 2' & gt; & Lt; / Div & gt; and loop through elements
$ ('popupDiv [data-layer]') each (function () {}); To differentiate between each group, you can use the following logic
// Processed Data-Layer Type Store Create an array to var dataArray = new array (); $ ('. PopupDiv') each (function () {var dataLayer = $ (this). Data ('Layer'); // Check that the data-layer is already processed if (! DataArray.indexOf (dataLayer)) {// Update Data Array DataArchash (DataLayer); $ ('. PopupDiv [Data-Layer =' '+ DataLayer +' "] '). Each (function () {// your stuff here}}}}; < / Code>
Comments
Post a Comment