"> " />
Easy Tutorial
❮ Jq Event Timestamp Misc Isemptyobject ❯

jQuery.parseHTML() Method

jQuery Miscellaneous Methods

Example

Create an array of DOM nodes from an HTML string and insert them into a div.

<div id="log">
    <h3>Content:</h3>
</div>
<script>
$(function () { 
    var $log = $( "#log" ),
        str = "hello, <b>my name is</b> jQuery.",
        html = $.parseHTML( str ),
        nodeNames = [];
    // Append the parsed HTML
    $log.append( html );
    // Collect the node names of the parsed HTML
    $.each( html, function( i, el ) {
        nodeNames[i] = "<li>" + el.nodeName + "</li>";
    });
    // Insert the node names
    $log.append( "<h3>Node Names:</h3>" );
    $( "<ol></ol>" )
        .append( nodeNames.join( "" ) )
        .appendTo( $log );
})
</script>

Definition and Usage

The $.parseHTML() function is used to parse an HTML string into an array of corresponding DOM nodes.

Note: 1. This function uses native DOM element creation functions to convert an HTML string into a collection of DOM elements, which you can then insert into the document. Security Consideration: Most jQuery APIs allow HTML strings that can contain running scripts within HTML. jQuery.parseHTML() does not run scripts in the parsed HTML unless you explicitly set the keepScripts parameter to true. However, scripts can still be executed indirectly in most environments, for example, through


Syntax

Parameter Description
htmlString String type The HTML string to be parsed and converted into an array of DOM nodes
context Element type Specifies in which Document the elements should be created, defaults to the current document's document
keepScripts Boolean type Specifies whether the input HTML string includes scripts, defaults to false

jQuery Miscellaneous Methods

❮ Jq Event Timestamp Misc Isemptyobject ❯