2

I want to find every "a" tag in this HTML string

$(document).ready(function(data) {
    $.get('test.html',
        function(responseText){
        //find each a tag   
        }, "html"
    )
});

Why is that so damn difficult for me ?

5 Answers 5

7

getting all links and doing something with them:

$.get('test.html', function(responseText) {
    var $response = $(responseText);
    var $links = $response.find('a');
    $links.each(function(index, $link) {
        // go nuts
    });
});

for more, read the jQuery documentation - its pretty good!

Sign up to request clarification or add additional context in comments.

1 Comment

cool, but how do I put the links a variable ? E.g var $links = $response.find('a');
3

I am not sure whether any of the code works if the responseText is a string.
You have to HtmlEncode it using jQuery first and then find your anchor tag.

Example:

$("<div/>").html(responseText).find('a').each(function(idx, elm) {
    //here are your anchors
    //alert(elm.href);
});

Working Demo: http://jsfiddle.net/naveen/Tcp3t/

Hope this helps.

Comments

3

try this:

$(responseText).find('a')

Comments

2

Create a jQuery object of the HTML and use .find():

$(responseText).find('a');

Comments

0

It seems that you get the right answer. But you could have faced a link in plain text without any reference tag to find it as I did.

I've found this jquery plugin that can indetify links inside a text and then create the tags

Hope It will help somebody

Jquery Linkify

Comments

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.