- Using the createElement() method
- Using the jQuery parseHTML() method
Approach 1: Using the createElement() method.
The createElement() method is used for creating elements within the DOM. It accepts two parameters, a tagName which is a string that defines the type of element to create, and an optional options object that can be used to modify how the element is created. Any element that is needed can be passed as a string in this function and this would return the specified element. This approach can only be used to create a single element from one string.
Example: In this example, we create a heading element by specifying the string as “h2”.
Approach 2: Using the jQuery parseHTML() method.
The parseHTML() method of jQuery is used to parse an HTML string so that it can be used to create elements according to the given HTML. This approach can be used to create multiple elements from the string.
Example: In this example, the string is specified with multiple elements that are parsed to HTML and added to the body of the document.
Please Login to comment...