Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy the functionality of outerHTML #54

Closed
AlbertoElias opened this issue Apr 19, 2012 · 4 comments
Closed

Copy the functionality of outerHTML #54

AlbertoElias opened this issue Apr 19, 2012 · 4 comments

Comments

@AlbertoElias
Copy link

I'm using cheerio on my node.js server, and using the method html() I can copy the functionality of innerHTML, but I haven't found a way to copy how outerHTML works. How could I do this?

Thanks :)

@matthewmueller
Copy link
Member

You can use $.html() to render the whole string or $.html([Cheerio Object]) to render the outerHTML of a specific element.

@AlbertoElias
Copy link
Author

The thing is, I'm using it inside an each function, like this:

$(element).each(function(index, elem) {
console.log($(this).html());
});

And in that case, it gives back the equivalent to innerHTML. I suppose this is because elem is a Cheerio Object instead of a string indicating the element. But I can't find how to manipulate this element to get the outer HTML. Thanks!

@matthewmueller
Copy link
Member

Oh okay, so you'll want to do something like this:

$(element).each(function(index, elem) {
  var $this = $(this);
  console.log($.html($this));
});

Keep in mind cheerio is try to emulate jQuery as close as possible so $(...).html() in jQuery returns innerHTML. $.html() is non-standard, but really useful for server-side HTML manipulation.

@AlbertoElias
Copy link
Author

Now I get it :P Thanks a lot for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants