Stocker dans un tableau tous les éléments d’une liste
Source: Get an array of list element contents in jQuery
.
1 2 |
var optionTexts = []; $("ul li").each(function() { optionTexts.push($(this).text()) }); |
Ajouter des classes à n divs en fonction du contenu de ce tableau
Source: How do I add class from array to n divs using .each function?
.
1 2 3 4 |
var colours = ['red', 'green', 'blue']; $('.box').each(function(index, element) { $(element).addClass(colours[index % colours.length]); }); |
Exemple:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<table id="relatedProductsTable" class="product-features-table is-full-width" tal:define="headerLine php: templateManager.getHeaderTable(lines[0])"> <thead> <tr> <th tal:repeat="columnTitle headerLine">${columnTitle}</th> </tr> </thead> <tbody> <tr tal:define="lines php:templateManager.getLineTable(lines, product)" tal:repeat="line lines"> <td data-title="" tal:repeat="value line"> ${value} </td> </tr> </tbody> </table> |
1 2 3 4 5 6 7 8 |
var relatedProductsTableHeadings = []; $('#relatedProductsTable thead tr th').each(function() { relatedProductsTableHeadings.push($(this).text()) }); $('#relatedProductsTable tbody tr td').each(function(index,element) { $(element).attr('data-title', relatedProductsTableHeadings[index % relatedProductsTableHeadings.length]); }); |
Résultat dans le DOM:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<table id="relatedProductsTable" class="product-features-table is-full-width"> <thead> <tr> <th>Bearing 1</th><th>Nut</th><th>Lock washer</th><th>Feltstrips (optional)</th> </tr> </thead> <tbody> <tr> <td data-title="Bearing 1"> 22220 </td><td data-title="Nut"> KM 20 </td><td data-title="Lock washer"> MB 20 </td><td data-title="Feltstrips (optional)"> 9x7,5x428 </td> </tr> </tbody> </table> |