YouTip LogoYouTip

Misc Parsehtml

# jQuery.parseHTML() Method [![Image 3: jQuery Misc Methods](#)jQuery Misc Methods](#) ## Example Create an array of DOM nodes from an HTML string and insert it into a div.

Content:

$(function(){var $log = $("#log"), str = "hello, my name is jQuery.", html = $.parseHTML(str), nodeNames = []; //Add the parsed HTML $log.append(html); //Collect the node names of the parsed HTML $.each(html, function(i, el){nodeNames = "
  • " + el.nodeName + "
  • "; }); // Insert the node names $log.append("

    Node Names:

    "); $("
      ") .append(nodeNames.join("")) .appendTo( $log); }) [Try it Β»](#) * * * ## 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 the HTML string into a collection of DOM elements, which you can then insert into the document. 2. If the context parameter is not specified, or is null or undefined, it defaults to the current document. If the created DOM elements are intended for another document (e.g., an iframe), you should specify that iframe's document object. **Security Considerations:** Most jQuery APIs allow HTML strings to contain scripts that run in the HTML. jQuery.parseHTML() will not run scripts within the parsed HTML unless you explicitly set the keepScripts parameter to true. However, most environments can still execute scripts indirectly, for example, via the attribute. Callers should avoid this and sanitize or escape any untrusted input from sources like URLs, cookies, etc., to prevent this. For future compatibility, when the keepScripts parameter is omitted or false, callers should not rely on the ability to run any script content. * * * ## Syntax _$_.parseHTML( htmlString [, context ] [, keepScripts ] ) | Parameter | Description | | :--- | :--- | | _htmlString_ | String type. The HTML string to parse and convert into an array of DOM nodes. | | _context_ | Element type. Specifies in which Document to create the elements. Defaults to the current document's document. | | _keepScripts_ | Boolean type. Specifies whether to include scripts in the passed HTML string. Defaults to false. | * * jQuery Misc Methods](#)
      ← Misc ParsexmlMisc Noop β†’