I'm trying to extract all links on a web page that has the following markup:
<a href="/item/0/100">0</a>
<a href="/item/1/100">2</a>
<a href="/item/2/100">3</a>
<a href="/item/3/100">4</a>
<a href="/item/4/100">5</a>
Basically returning all the /item... paths. I have the dom object that contains this. Any idea how to do this?
Thanks!
EDIT: Using jQuery with Map returns (truncated)
http:undefined
{ '0': '/item/200/13/0',
'1': '/item/200/1/0',
'2': '/item/200/4/0',
'3': '/item/200/5/0',
'4': '/item/200/11/0',
length: 4,
prevObject:
{ '0':
{ _ownerDocument: [Object],
_childNodes: [Object],
_attributes: [Object],
_nodeName: 'a',
_childrenList: null,
_version: 3,
_nodeValue: null,
_parentNode: [Object],
_readonly: false,
_tagName: 'a',
_created: true,
_attached: true,
_attachedToDocument: true },
'1':
...
Newer browsers:
var links = document.querySelectorAll('a[href^="/item/"]');
Older browsers:
var links = [];
var elements = document.getElementsByTagName('a');
for (var i = 0; i < elements.length; i++) {
var a = elements[i];
if (a.getAttribute('href').indexOf('/item/') === 0) {
links.push(a);
}
}