-2

Assume I have the following HTML

<articles>

    <article id="a1">
        <!-- Content of article with id="a1" -->
    </article>

    <article id="a2">
        <!-- Content of article with id="a2" -->
    </article>

    <article id="a3">
        <!-- Content of article with id="a3" -->
    </article>

</articles>

Using jQuery, I did the following

var x = $("#a2").html();

Now, the variable x will contian:

<!-- Content of article with id="a2" -->

However, I like to have x contain:

<article id="a2">
    <!-- Content of article with id="a2" -->
</article>

I tried:

var x = $("#a2").parent().html();

but this returned all three articles, which is not what I want. I only want article a2. How can I do that?

Thanks.

Greeso
  • 7,544
  • 9
  • 51
  • 77

1 Answers1

1

Try to use the .outerHTML property,

var x = $("#a2")[0].outerHTML;
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130