1

I know there are many different ways for creating HTML elements in jQuery, but I've seen people either showing:

$("<div></div>", {attrs...})

or

$("<div/>", {attrs...})

as the primary methods for doing so.

I've found that

$("<div>", {attrs...})

works just fine too.

Why does no one ever show the third method? Are there potential problems with it? As far as the result it concerened, it looks much the same when I inspect element to see the created div.

4
  • The validity varies based on browsers. Commented Jan 16, 2013 at 6:31
  • I use the third method all the time without any problems. Commented Jan 16, 2013 at 6:32
  • There is nothing wrong with the third version. Ultimately jQuery recognizes that you want to create a new element. I would assume people don't use that notation because it can be confused easily with an actual selector. Commented Jan 16, 2013 at 6:33
  • I am using .html(), append(), replaceWith(), insertBefore() and insertAfteR() to create html elements. Commented Jan 16, 2013 at 6:34

4 Answers 4

3

Creating the html element with tag like $('<div>'), $("<div>", {attrs...}) is documented way of creating html elmenets check it here.

When the parameter has a single tag (with optional closing tag or quick-closing) — $("<img />") or $("<img>"), $( "<a></a>" ) or $("<a>" ) — jQuery creates the element using the native JavaScript createElement() function.

Sign up to request clarification or add additional context in comments.

Comments

2

Lets look into jQuery source code:

https://github.com/jquery/jquery/blob/master/src/core.js#L85

// Handle HTML strings
if (typeof selector === "string") {
  if (selector.charAt(0) === "<" && selector.charAt(selector.length - 1) === ">" && selector.length >= 3) {
    // Assume that strings that start and end with <> are HTML and skip the regex check
    match = [null, selector, null];
  }
  // ...
}

So the string just must start with < and end with > to be considered as HTML. All of this variants are valid. I think the reason that <div> is least popular is that it reminds people unclosed tag which is not valid HTML. However jQuery accept it.

Comments

1

If you're doing $('<div>'), jQuery will also use document.createElement().

(Just take a look at http://code.jquery.com/jquery-1.4.2.js, line 117).

There is some function-call overhead, but unless performance is critical (you're creating hundreds [thousands] of elements), there isn't much reason to revert to plain DOM.

Just creating elements for a new webpage is probably a case in which you'll best stick to the jQuery way of doing things.

Comments

0

As per jQuery documentation : To ensure cross-platform compatibility, the snippet must be well-formed. Tags that can contain other elements should be paired with a closing tag.

$( "<div></div>" ); 

But tags that cannot contain elements may be quick-closed or not as follows:

$( "<img />" );
$( "<input>" );

Although <div>, </div> might be working for you in some browser but result may vary browser to browser and even across different versions of same browser.For safe side always stick to standards and documentation best practices.

1 Comment

Tags that contain other elements should be well-formed and thus paired with a closing tag. For single elements it doesn't matter, jQuery treats $('<div>', {attrs...}) exactly the same as the alternatives.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.