Pranav C Balan
Pranav C Balan

Reputation: 115222

Unexpected behavior of html()

JQuery 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="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div></div>

The above code binds two divs.

Is array of jQuery object equals to jQuery object?

Upvotes: 2

Views: 70

Answers (1)

guest271314
guest271314

Reputation: 1

.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 );
  }

Upvotes: 2

Related Questions