tags:

views:

339

answers:

3

Say I have the following html:

<div id="myDiv" title="My Div">This is me!</div>

I want to write a jquery statement such that the result is the entire line above as a string.

I almost want something like: var selectedHtml = $("#myDiv").self(); (I know that's not valid jquery) which results in selectedHtml having the value "<div id="myDiv" title="My Div">This is me!</div>"

Any ideas about which jquery function I'm looking for?

PS: getting the .html() of this node's .parent() will not work as it will give me siblings of the above node too.

+2  A: 

You can use plain javascript to do this.

var selectedHtml = document.getElementById ( "myDiv" ).outerHTML; //IE only

You can use this to get the outerHTML of a jquery element

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

From

jQuery Snippets: outerHTML

rahul
close, but this breaks down if I don't want the result as a `<div>`. What if the element I select is a `<tr>` and I don't want it wrapped in a `<div>`. Or am I missing something?
psychotik
No you can call $("#myDiv").outerHTML() and you will get the desired result.
rahul
Here's what I meant - what if in my question the html in question was `<tr id="myTr" title="My Tr">This is my Tr</tr>`. Do you agree that `$("#myTr").outerHTML() will return the wrong result?
psychotik
You can call this on any jQuery element.
rahul
+4  A: 

I think might also be possible:

  • wrap your div with another div (<div id="wrapdiv" />) --> wrap()
  • get the html on the wrapdiv
  • unwrap the wrapdiv (so do $("#myDiv").unwrap()) --> unwrap()

Here is a live example: http://jsbin.com/oniki/edit

edit:

Perhaps this is better because it doesn't use an id for the wrapperdiv and therefore less change for bugs with existing ids:

  • wrap() your div with another (nameless) div
  • get the html() of the parent() of your div
  • unwrap() your div

See it in action: http://jsbin.com/oniki/3/edit

Natrium
nice. this looks promising...
psychotik
I wonder how browsers handle it if you wrap an element in a `div` when that's not valid structure, like wrapping a `td`, `tr`, `li`, etc. Still, good thought and probably better than cloning.
T.J. Crowder
@T.J. Crowder, I tried it with an unclosed `<li>` and firefox 3.6 handles it fine: `<li id="test">hello world</li>`
Natrium
Yeah, as long as the OP tests it in their target browsers with the kinds of elements they're expecting to use, and it does work, this is my favorite approach.
T.J. Crowder
A: 

Found this - very complete, but I'm not sure if it's overkill...

jQuery.fn.extend( {
outerHtml: function( replacement )
{
 // We just want to replace the entire node and contents with
 // some new html value
 if (replacement)
 {
  return this.each(function (){ $(this).replaceWith(replacement); });
 }

 /*
  * Now, clone the node, we want a duplicate so we don't remove
  * the contents from the DOM. Then append the cloned node to
  * an anonymous div.
  * Once you have the anonymous div, you can get the innerHtml,
  * which includes the original tag.
  */
 var tmp_node = $("<div></div>").append( $(this).clone() );
 var markup = tmp_node.html();

 // Don't forget to clean up or we will leak memory.
 tmp_node.remove();
  return markup;
 }
});

http://ivorycity.com/blog/2009/08/18/3/

Raithlin