What does the selector syntax mean in this code? I\'ve seen selectors like
div
or #someId
but I\'m confused what the
$('<div/>')
will not select a div from your html, but create a new one. Also it can be written like $('<div></div>')
, first one is just a shorthand for second.
<div />
indicates that the div is a self closing div. It's basically shorthand for $('<div></div>')
.
Any browser that supports XHTML supports the self-closing syntax on all elements.
It means that the jquery function will not actually query for DIV elements but will create an jquery wrapped element of type <div></div>
on which you can work on and append to the DOM eventually.
In your case, the code will create jquery object representing a DIV element, set its innerText to what the message variable contains and prepend it to the element with ID "log".
It means "create a jQuery-wrapped div element on the fly".
See http://api.jquery.com/jQuery/
From the above:
jQuery( html [, ownerDocument] )
Description: Creates DOM elements on the fly from the provided string of raw HTML.
Later...
When the parameter has a single tag, such as
$('<img />')
or$('<a></a>')
, jQuery creates the element using the native JavaScriptcreateElement()
function.
So basically, it's like doing:
$(document.createElement("div")).text("blahblah");
It will create a new <div/>
element and prepend it to the #log
element.
It creates a new div
tag and prepends it to log
.