jQuery.parseHTML()S2C Home « Utilities « jQuery.parseHTML()

Parsed data to HTML node creation.

Description

The jQuery.parseHTML() jQuery General utility method, parses a string into an array of DOM nodes.

Shorthand version $.parseHTML()

  • A native DOM element creation function is used to convert the parsed string to a set of DOM elements. These can then be inserted into the document or optionally a context within the document whilst retaining any user scripts present when required.

Syntax

Signature Description
jQuery.parseHTML(data [,context ] [,keepScripts ] )Parse a string into an array of DOM nodes, optionally using a context within the document whilst retaining any user scripts present when required.

Parameters

Parameter Description Type
dataA well-formed HTML string to be parsed.String
contextAn optional DOM element, which will serve as the context, into which the HTML fragment will be created.
  • Default: document
Element
keepScriptsAn optional Boolean indicating whether to include scripts passed in the HTML string.
  • Default: false
Boolean

Return

An Array object.

jQuery.parseHTML(data [,context] [,keepScripts]) ExampleTop

Parses a string into an array of DOM nodes, optionally using a context within the document whilst retaining any user scripts present when required.

In the example below when we press the button the first time we parse some well formed HTML to the division with an id of 'div19' and then print out the DOM nodes.



$(function(){
  $('#btn23').one('click', function(){
    // Add some HTML
    parsedHtml = $.parseHTML( "Sentence has <strong>strongly typed text</strong> in it.<br>" ); 
    $('#div19').append(parsedHtml);
    // Append parsed HTML node names
    $.each( parsedHtml, function( i, el ) {
      $('#div19').append( el.nodeName + "<br>" );
    });
  });
});

div19. Our parsed HTML and nodes appear after 'btn23' is clicked.

Press the button below to action the above code: