jQuery.parseHTML() JQ Home  <<  Utilities  <<  jQuery.parseHTML()

Parsed data to XML document 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 XML string to be parsed.String

Return

An XML document.

jQuery.parseHTML( data ) Example Utilities  <<  Top

Parse a string into an array of DOM nodes.

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 <b>strongly typed text</b> 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:

Related Tutorials

jQuery Intermediate Tutorials - Lesson 9 - jQuery General Utilities