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

.html() returns outerHTML, while .html(html) sets innerHTML #16

Closed
matthewmueller opened this issue Dec 19, 2011 · 8 comments
Closed

.html() returns outerHTML, while .html(html) sets innerHTML #16

matthewmueller opened this issue Dec 19, 2011 · 8 comments

Comments

@matthewmueller
Copy link
Member

I can't believe this one has gone unnoticed. I'm not really sure what to do about it actually. I think it's more useful how it is right now, but I don't like the asymmetry. I also don't like straying away from the jQuery API.

What do you guy's think?

@siddMahen
Copy link

I think that we should stick to the jQuery spec. So .html() should both return and set innerHTML.

@matthewmueller
Copy link
Member Author

Yah.. but jQuery doesn't have a good way of getting "outerHTML".

@siddMahen
Copy link

Then it would be better to provide a separate function to get and set outerHTML, rather than mix and match with .html().

Plus, it's better to break functionality early on, rather than later.

@ironchefpython
Copy link

I'd like to be able to use this library to execute the same manipulation on the server as I do in the browser. jQuery compatibility seems like the highest priority. I think .html() should read and write innerHTML.

I'd vote for adding a special method like .toHtml() which would return an array of strings if you need to dump outerHTML of the collection for debugging purposes.

@matthewmueller
Copy link
Member Author

Yah, I tend to agree with you. I want it to be incredibly easy to get the outer HTML though, because most of my use cases involve manipulating the html string, then rendering the whole string.

I'm realizing though, that in jQuery i never needed to get the outer HTML, because I'm never rendering the whole document. So this is what I'm thinking:

$('.orange').html() // => returned innerHTML

$.html() // => Returned entire HTML string

This way we are adhering to the jQuery spec while extending the spec gracefully to cover a major use case of server-side rendering.

@ironchefpython
Copy link

I'm fine with that, also, I'd allow $.html to take an optional cheerio parameter, so in code like the following:

var span = $("<div><span>foo</span><span>bar</span></div>").children().get(1);
span.html() // => returns "bar"
$.html(span) // => returns "<span>bar</span>"

The major advantage to that, is if I wanted jQuery to behave the same way, I could write a jQuery plugin for that in 30 seconds.

@matthewmueller
Copy link
Member Author

Jeez @ironchefpython you're on fire! I like it. Added to master.

@ironchefpython
Copy link

The following assertion fails:

      $.html($('<span>baz</span>')).should.equal('<span>baz</span>');

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

No branches or pull requests

3 participants