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 |
---|---|---|
data | A well-formed HTML string to be parsed. | String |
context | An optional DOM element, which will serve as the context, into which the HTML fragment will be created.
|
Element |
keepScripts | An optional Boolean indicating whether to include scripts passed in the HTML string.
|
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.
Related Tutorials
jQuery Intermediate Tutorials - Lesson 9 - jQuery General Utilities