I have a div and a list with some items in it:
<div id="myDiv">
<ul>
<li><a>Item 1</a></li>
<li><a>Item 2</a></li>
<li><a>Item 3</a></li>
</ul>
How could I easily make a Javascript array of the ‘a’ elements knowing that the number of items may change?
If you want a static list:
But you wrote “…knowing that the number of items may change”. If this means you want a “live list” that updates with DOM changes, then do this:
This list will update when items are added or removed. It makes the assumption that there are no other
<a>elements in the<div>. If there are, you’ll need to adjust.