Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  124] [ 4]  / answers: 1 / hits: 80608  / 9 Years ago, fri, september 18, 2015, 12:00:00

I need to parse some markup similar to this one, from an html page:



<div id=list>

<div class=item-level-a>
<div class=item-level-b>
<a href=http://www.example.com/1></a>
</div>
</div>

<div class=item-level-a>
<div class=item-level-b>
<a href=http://www.example.com/2></a>
</div>
</div>

<div class=item-level-a>
<div class=item-level-b>
<a href=http://www.example.com/3></a>
</div>
</div>

</div>


I did try with this code:



$list = [];
$('div[id=list]').each(function() {
var href = $(this).find('div > div > a').attribs('href');
list.push(href);
});


without success: error was:



TypeError: Object <a href=http://www.example.com/1></a>
<a href=http://www.example.com/2></a>
<a href=http://www.example.com/3></a>
has no method 'attribs'


:-(.



Any clue?


More From » node.js

 Answers
107

In cheerio and jquery, you get attributes with attr(), not attrib().



There are a few other problems with your code. Here is a working version using cheerio. It probably works in jquery this way as well.:



var list = [];
$('div[id=list]').find('div > div > a').each(function (index, element) {
list.push($(element).attr('href'));
});
console.dir(list);

[#65006] Thursday, September 17, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;