Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are jQuery angle bracket selector looking things, like $('<img/>')?

Sometimes I see constructs like $('<img/>'). How is $('<img/>') different from $('img') and where can I read more about this?

I tried looking at jQuery Selectors but found nothing related to this format.

like image 750
ivavid Avatar asked Apr 11 '13 14:04

ivavid


People also ask

What do the jQuery selectors in this code select?

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.


1 Answers

The jQuery function is overloaded to construct new jQuery elements when passed a string that looks like HTML. From the docs:

If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (i.e., it starts with <tag ... >). If not, the string is interpreted as a selector expression, as explained above. 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.

like image 179
jbabey Avatar answered Sep 22 '22 21:09

jbabey