24

I use jQuery method to get some type of html object:

var content = $('#cke_ckeditor iframe').contents().find('.cke_show_borders').clone();

Then I want to convert it to string type:

console.log(content[0].toString());

but the result is:

[object HTMLBodyElement]

How can I turn it into real string?

By the way, can I turn the converted html string to the html object?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
hh54188
  • 14,887
  • 32
  • 113
  • 184

7 Answers7

53

I believe you want to use Element.outerHTML:

console.log(content.outerHTML)

Mamun
  • 66,969
  • 9
  • 47
  • 59
Chris
  • 2,537
  • 1
  • 25
  • 22
3

I had the same problem.

var docString = "<html>"+content.documentElement.innerHTML+"</html>"

E. Nat
  • 31
  • 2
2

You can use this content[0].prop('outerHTML')

It worked for me

Reference : How do you convert a jQuery object into a string?

Community
  • 1
  • 1
Cybersupernova
  • 1,833
  • 1
  • 20
  • 37
1

You can try the following:

content.text();
jdelobel
  • 263
  • 2
  • 7
1

The correct way to Convert a jQuery object into a string:

var content = $('#cke_ckeditor').find('.cke_show_borders').eq(0).clone();

console.log(content.get(0).outerHTML);

Vijay Maurya
  • 99
  • 1
  • 6
0

'outerHTML' working for me.

In Google Tag Manager 'custom javasctipt' part {{Click Element}} returns [object HTMLElement].

function() {
   var elm = {{Click Element}};
   return elm.outerHTML; // working
}

Result -

<i _ngcontent-c1="" class="material-icons">edit</i>
Pinaki
  • 792
  • 8
  • 17
0

You can use content.innerHTML This will solve your issue

Akshay Shrivastav
  • 1,115
  • 4
  • 17
  • 43