Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$('<element>') vs $('<element />') in jQuery

Tags:

jquery

I see people creating HTML elements in jQuery in two different ways:

$('<element>') 

and

$('<element />')  

I am curious which one is "more correct". I see the obvious advantage to the first one as being just simply less to type. Does it make a difference at all which one is used?

like image 707
GoldenNewby Avatar asked Mar 12 '12 22:03

GoldenNewby


People also ask

What are elements in jQuery?

jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.

What is the $() in jQuery ()?

When a jQuery object is passed to the $() function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.

What is append and prepend in jQuery?

append() & . prepend() .append() puts data inside an element at the last index; while. . prepend() puts the prepending element at the first index.


1 Answers

There is no difference, as seen in the source code, line 30 & line 121:

/* Line 30*/ rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,  /* Line 121 */ // If a single string is passed in and it's a single tag // just do a createElement and skip the rest ret = rsingleTag.exec( selector ); 

The following are equivalent:

  • <a></a>
  • <a />
  • <a>
like image 112
Rob W Avatar answered Sep 23 '22 17:09

Rob W