Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the html object to string type?

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?

like image 743
hh54188 Avatar asked Sep 10 '12 02:09

hh54188


People also ask

How do I turn a HTML element into a string?

To convert a HTMLElement to a string with JavaScript, we can use the outerHTML property. const element = document. getElementById("new-element-1"); const elementHtml = element.

How do you turn an object into a string?

We can convert Object to String in java using toString() method of Object class or String. valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else.

How do you turn an object into a string in JavaScript?

Stringify a JavaScript ObjectUse the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

Which of the function is used to convert object to string?

The answer is option D (str(x)). str(x) converts the object to a string in python.


3 Answers

I believe you want to use Element.outerHTML:

console.log(content.outerHTML)

like image 198
Chris Avatar answered Sep 22 '22 11:09

Chris


I had the same problem.

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

like image 27
E. Nat Avatar answered Sep 25 '22 11:09

E. Nat


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

It worked for me

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

like image 32
Cybersupernova Avatar answered Sep 25 '22 11:09

Cybersupernova