Somenath Sinha Somenath Sinha - 4 months ago 11
HTML Question

in jQuery, what is the difference between $("div") and $("<div>")?

I have a code in a book which reads:

function hit()
{
var $d = $("<div>");
$d.addClass("current_hand")
.appendTo("#my_hand");
}


I know that
$("div")
will select all the div elements. However what does
$("<div>")
do? Does it create a new div element? If so, is this the standard way of creating new DOM elements in jQuery, or are there other ways?

Answer

in jQuery, what is the difference between $("div") and $("<div>")?

$("div") finds all existing div elements in the document.

$("<div>") creates a div element, which you'd then append to the document at some stage (presumably).

If so, is this the standard way of creating new DOM elements in jQuery, or are there other ways?

Fairly standard, yes. Other ways of creating elements include adding HTML content to any existing element (via append or html or before or insertBefore or any of a large number of other functions).