What does $ (document.body) mean in javascript?

What does it mean?

+4
source share
6 answers

$ is the name of the function. It is passed the DOM element of the document document. Usually $ is used to represent the JavaScript library. Most often jQuery . In jQuery, it selects the body element.

+6
source

document.body in javascript is a direct reference to the DOM element representing the <body> the page.

The $() depends on how it is used. $ may be the name of the variable and () after the variable or property name attempts to call the function stored in this variable or property.

So, if you have:

 var $ = function() { alert('howdy'); }; 

Then this:

 $(); 

... will call this function and cause a warning.

Functions can take arguments, so you can change the function above to accept the document.body element as an argument, and alert() its innerHTML (for example);

  // alerts the innerHTML of the element it receives var $ = function( elem ) { alert( elem.innerHTML ); }; $( document.body ); // Passes the element when calling the $ function 
+9
source

it passes the DOMElement reference to the jQuery object / function, so that the jQuery object is returned, where [0] contains the link, and the context contains bodies.

0
source

I think this makes sense in jquery or prototype (or other frameworks), not pure javascript. $ is a function; in the prototype, it extends document.body using framework methods.

0
source

You just pass an argument to a function named "$"

 function $(someargument){ .... } 

In this case, the argument passed is document.body

Jquery usually uses $, so in this case, probably someone wanted to use the Jquery functions directly on the body, i.e. wrap the body in jquery.

 $(document.body).html("hi"); 

(maybe this is not a good idea, but you understand)

0
source

view SymbolHound results for "$ (document.body)"

0
source

All Articles