views:

106

answers:

3

I have the following structure in my document:

<div id="clientList>
<a href=whatever.php" att="something">Text</a>
<a href=whatever.php" att="something">Text</a>
</div>

Say I want to get all of the links between the div out, how would I do this?

I tried the following:

$('#clientList').children("a").each(function() {
var x = $(this).html();
});

But I don't get the full link, all I get is the "Text" part in between the links. How can I get the full HTML string of each link, including the href and other attributes?

+6  A: 

I believe this is a dupe of http://stackoverflow.com/questions/1526407/jquery-select-html-of-an-element-inclusive

Anyway, as mentioned there, you should be able to write an outerHTML plugin with:

jQuery.fn.outerHTML = function() { 
    return $('<a>').append(this.eq(0).clone()).html(); 
}; 

and then use it as

$(this).outerHTML();
Brian Hasden
A: 

If your using jquery 1.4 you can use that unwrap() I have not played much around with it yet but this is what I got so far.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">

        $(function()
        {
            var h = $("#clientList a").unwrap();
            jQuery.each(h, function(i, val)
            {

                alert(val);

            });
        });
    </script>
</head>
<body>
    <div id="clientList">
    <a href=whatever.php" att="something">Text</a>
    <a href=whatever.php" att="something">Text</a>
    </div>
</body>
</html>

this will get you the full href path. Right now it does not get the '' attribute right now. Not sure how to do it.

chobo2
+1  A: 

have you tried? :

 $('#clientList').children('a').each(function() {
            alert($(this)[0].outerHTML);
        });
FrenchiInLa
`outerHTML` is not standard -- and AFAIK is only supported by IE.
J-P
`outerHTML` is a non-standard IE extension. It won't work in Firefox (or some of the other browsers). [hah! snap.]
bobince