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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With