[Solved-6 Solutions] jQuery document.createElement equivalent - javascript tutorial
Problem:
How jQuery document createElement equivalent ?
Solution 1:
Here is a couple of examples are given below :
Solution 2:
Use this code:
To create a DIV element in jQuery.
Solution 3:
Use this code:
Solution 4:
HTML elements needs to add in a jQuery constructor $() will return a jQuery object to built HTML, is suitable for being appended into the DOM by using jQuery's append()
method.
To specify any arbitrary HTML, including class names or other attributes, to find more concise than using createElement and then setting attributes like cellSpacing and className via JS.
Read Also
Undefined Object PropertySolution 5:
- If we use
$(string)
, jQuery will examine the string need to select a html tag or create a new element. By using$.parseHTML()
, in jQuery want to create a new element.
Using $.parseHTML()
:
Note that a is a jQuery object while b is a html element:
Solution 6:
Using jquery, don't assign properties passed to the second object in the following method. Using document.createElement('div')
together with jQuery
is faster