Pranav C Balan Pranav C Balan - 1 year ago 85
Javascript Question

Unexpected behavior of html()


method takes arguments html string or a function as per it's documentation. But if I used array of jQuery objects it also works with it. How it's works there is no documentation at all.

For example :

$('div').html([$('<div/>', {
text: '1'
}), $('<div/>', {
text: '2'

<script src=""></script>

The above code binds two divs.

Is array of jQuery object equals to jQuery object?

Answer Source

.html() accepts jQuery object as parameter

How it's works there is no documentation at all

This is correct. This is not thoroughly described at jQuery documentation. You would need to review the source of jQuery to be aware of this; without trying passing a jQuery, or other object, for example an array of jQuery objects to .html().

See jquery-2.2.3.js at 5490 - 5527 for jQuery.fn.html function; note if condition and use of .append() at 5523 - 5525

  if ( elem ) {
    this.empty().append( value );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download