Reputation: 5237
I am working on a google chrome extension. I extracted the source code of the webpage using a jquery get function in the following way:
$.get(link,function(data){
//From this the entire source code of the link will be available in the variable data
};
The html source will be of this format
<div class="DivClass">
<img src="2.jpg" width="100" />
<span class="time_date">FEBRUARY 19, 2014</span>
<h3><a title="Title" href="213.html">Title</a></h3>
</div>
I wanted a way to extract the href link within the divclass named DivClass. I tried the following way:
var links=[];
$(data).find('#DivClass').each(function(){
links.push($(this).attr('href'));
});
I used links as an array, as there can be multiple instances of the specific divclass. Could someone suggest where I am going wrong?
Upvotes: 0
Views: 346
Reputation: 1923
Because the #DivClass
is not the link. Try this:
var links=[];
$(data).find('.DivClass a').each(function(){
links.push($(this).attr('href'));
});
Upvotes: 1
Reputation: 7367
You are using id selector (#
) for class (.
)
$(data).find('.DivClass a').each(function(){
links.push($(this).attr('href'));
});
Upvotes: 1
Reputation: 133403
You need to use class selectors. So use .DivClass
instead of #DivClass
and You need to target anchor thus change your selectors to $(data).find('.DivClass a')
Use
var links = [];
$(data).find('.DivClass a').each(function () {
links.push($(this).attr('href'));
});
Upvotes: 2