The reason is that we can do
$('<div>some text</div>').prependTo('#someDiv')
so, this is also ok to do:
$('some text').prependTo('#someDiv')
but we can't change some text to body? body is just as good as any text...
(the above code is adding some text to the div with id someDiv, so what if I want to add the word body to the div?)
but $('body') becomes a selector for the body element... so is there a rule that says, we can use any text as HTML code, so long as it is not the name of HTML elements?
Projects In JavaScript & JQuerytext() – This method sets or returns the text content of elements selected. html() – This method sets or returns the content of elements selected.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
The :button selector selects button elements, and input elements with type=button.
Basically this is never valid:
$('generic text here, *not* a selector or HTML');
That should always be either a selector (finding an element) or html (creating element(s)), you can view the API for $() here.
You want to use one of the other DOM Insertion methods here, .prepend() in this case:
$('#someDiv').prepend('any text or html here is valid, <b>Hey look bold</b>.');
If it contains html tags then it is treated as html (except it strips text before the first tag and after the last). eg $("<div>some text</div>"). Otherwise it is treated as a selector, eg $("some text").
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