jack onsl
jack onsl

Reputation: 63

what is jQuery <div> selector

im new to jQuery and i come to a line i cant understand and i see lots using it,

anyone can explain this:

var div = jQuery("<div>", { "class": "someClass" });

Upvotes: 0

Views: 482

Answers (1)

adeneo
adeneo

Reputation: 318322

If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (i.e., it has <tag ... > somewhere within the string).

If not, the string is interpreted as a selector expression. But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML.

Then a jQuery object is created and returned that refers to these elements. You can perform any of the usual jQuery methods on this object.

As for the second argument to $(), as of jQuery 1.4 the $() selector can also accept a map consisting of a superset of the properties that can be passed to the .attr() method.

Furthermore, any event type can be passed in, and the following jQuery methods can be called: val, css, html, text, data, width, height, or offset.

The name "class" must be quoted in the map since it is a JavaScript reserved word, and "className" cannot be used since it is not the correct attribute name.

jQuery selectors documentation!

Upvotes: 4

Related Questions