Copy the functionality of outerHTML #54

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

2 participants

@AlbertoElias

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

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

@AlbertoElias

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

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

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