0

I posted this up a day or two ago, received an answer which worked on JSfiddle, but didn't work on my actual code.

  <div id="PublicResults">
<h2>
<dl>
<dt>
<a onmouseout="swho.alone.OnMouseOut(event)" onmouseover="swho.alone.showPopup(event, 'DR647E481', 'pra**', '', '', 'everyone', 'closed')" href="/SWApp/detailAction.do?key=DR647E481&search=pra**&soundex=&stanfordonly=&affilfilter=everyone&filters=closed">Ajay Prakash</a>
</dt>

Now, I tried $jq("#PublicResults a").attr("href"), which worked in jsfiddle, but isn't working on the page (my console reads undefined). However, when I try $jq("#PublicResults a[href]").attr("href"), I get the url of the current webpage- so I guess the issue is that .attr() is returning only the first element

So my question is twofold- if what I wrote just above is correct, how do I make .attr() return all elements. If not, what should I do to retrieve the url in href?

0

3 Answers 3

2

You have to loop troughout the array list of all links do you want to scraping:

$jq("#PublicResults a").each(function()
{
   alert($(this).attr("href"));
});
0

If you have your jQuery instance tied to $jq() (instead of the usual $() or jQuery()), then $jq("#PublicResults a[href]").attr("href") should work. Could you post a link to where it doesn't work?

I suspect one of the following:

  1. You may be running the jQuery code before the #PublicResults element is added to the page
  2. It is possible that the $jq() function is not available
  3. Something else may be altering your link href
1
  • er the $jq function works when I do "$jq("#PublicResults a[href]"), and I 'm running the code on a console in firebug to test it
    – praks5432
    Aug 29, 2011 at 10:45
0

You can use map to extract all of the href's from the jQuery wrapped objects. eg.

$('...').map(function() { return $(this).attr('href'); });

This returns a jQuery wrapped array, to get a normal JavaScript one, just use .get().

1
  • also, how do I load the url from which I'm scraping with this?
    – praks5432
    Aug 29, 2011 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.