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 ?

danielovich
  • 9,217
  • 7
  • 26
  • 28

5 Answers5

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!

jrharshath
  • 25,975
  • 33
  • 97
  • 127
3

try this:

$(responseText).find('a')
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
James Dykes
  • 109
  • 5
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.

Community
  • 1
  • 1
naveen
  • 53,448
  • 46
  • 161
  • 251
2

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

$(responseText).find('a');
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
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

nach
  • 349
  • 1
  • 3
  • 10