tags:

views:

859

answers:

2

My code looks like this:

<ul id="ulList">
  <li class="listClass" id="id1"><a href="http://link1"&gt;Link 1</a></li>
  <li class="listClass" id="id2"><a href="http://link2"&gt;Link 2</a></li>
  <li class="listClass" id="id3"><a href="http://link3"&gt;Link 3</a></li>
</ul>

Now I like to get the following:

All links as an array

All ids of li as an array

Can someone help me please?

+2  A: 

This should work.

var ids = [],
    hrefs = []
;   
$('#ulList')
    .find('a[href]')  // only target <a>s which have a href attribute
        .each(function() {
            hrefs.push(this.href);
        })
    .end()
    .find('li[id]')   // only target <li>s which have an id attribute
        .each(function() {
            ids.push(this.id);
        })
;

// ids = ['id1', 'id2', 'id3']
// hrefs = ['http://link1', 'http://link2', 'http://link3']
nickf
+4  A: 
var ids = new Array();
var hrefs = new Array();
$('#ulList li').each(function(){
  ids.push($(this).attr('id'));
  hrefs.push($(this).find('a').attr('href'));
})
toby