0

In the following code want to remove the html and get it in a variable.How to go about this.

<div id="tr1">Server</div>
var t = $("#tr1").remove();
console.log(t); // Should print     <div id="tr1">Server</div>

OR should .detach() be used

Rajeev
  • 44,985
  • 76
  • 186
  • 285
  • `
      Server
    ` isn't valid HTML markup
    – A. Wolff May 11 '14 at 15:17
  • Then use: `console.log(t.prop('outerHTML'));` if you want the output to be a string http://jsfiddle.net/t354y/ Now i'm quite sure to don't understand what is your issue... Maybe you could provide a jsFiddle to make it clearer – A. Wolff May 11 '14 at 15:19

2 Answers2

1

To get the element HTML as string, you can use the outerHTML property of the DOM node:

jQuery:

console.log(t.prop('outerHTML'));

Or using DOM node:

console.log(t[0].outerHTML);
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

I don't think remove() will return you the HTML code when you delete the element.

To get the html code maybe you should look this answer: https://stackoverflow.com/a/8645992/3133256 (the updated)

Community
  • 1
  • 1
jpadilladev
  • 1,756
  • 4
  • 16
  • 23