0

Hi I have extracted the html source code from a website using jquery. I am trying to extract link under a specific tag which is of the form <p class="title"> <a href="some link "> I am trying to extract the link under a href. To extract the html i did

$.get("link",function(data){ alert($data('p').attr('title')); }

data contains the html source code. The alert box shows as undefined. Is it not possible to extract the

tag with data in this format? I am not able to get how to extract the link under the href tag. Please help

2
  • you are using data as function parameter an $data in your alert. These are 2 different variables... Commented Aug 3, 2012 at 19:13
  • 1
    Remember, "link" has to be on your same domain. Commented Aug 3, 2012 at 19:14

2 Answers 2

3

Original Solution (finds first link)

$.get("link",function(data){ alert($(data).find('p.title a').attr('href')); }

Modified Solution (finds all links)

$.get("link", function(data) {
    var links = [];

    $(data).find('p.title a').each(function() {
        links.push($(this).attr('href'));
    });

    alert(links.join(', '));
});
Sign up to request clarification or add additional context in comments.

8 Comments

it works..but when there are multiple p tags with class title it extracts only the 1st one
You are going to have to loop through each one to get them then. So $(data).find('p.title a').each(function(){ alert($(this).attr('href'));});
I added a separator argument to the join() method call. I do not think you would need that, but just in case should try that.
its working now, one bracket missing was the problem ! Thank you so much for your help :)
No problem, it gets confusing sometimes when you are using jQuery + AJAX :) Glad you got it working.
|
1

From your example it looks like you should be doing this:

$.get("link",function(data){ alert($(data).find('p.title a').attr('href')); }

2 Comments

Never done that before. We literally have the exact same answer :)
it works..but when there are multiple p tags with class title it extracts only the 1st one

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.