Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the vanilla JS equivalent of jQuery's $(document)?

Tags:

I'm trying to figure out the vanilla equivalent of the following code:

$(document).attr('key', 'value'); 

So far I've looked into

  • document - it's not an element so you cannot call setAttribute on it
  • document.documentElement - returns the html tag. This is not the same "element" that jquery is targeting
  • $(document)[0] seems to return a shadow element in Chrome Inspector
  • $(document).attr('key', 'somethingUnique') doesn't exist in the Chrome Inspector

Is jQuery creating it's own shadow element mock of the document so it can treat it like a real element? What element is jQuery actually referencing when you do $(document)?

like image 561
CTS_AE Avatar asked May 31 '18 19:05

CTS_AE


People also ask

What is $( document ready () equivalent in JavaScript?

jQuery $(document). ready() Equivalent in JavaScript This event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

What is document ready in VanillaJS?

The vanilla JavaScript DOMContentLoaded event is fired when the DOM is ready, but there could still be resources that are being loaded (e.g. images, external stylesheets, etc). If you want to instead start manipulating elements when the full page has loaded, you can use the load event.

What is vanilla jQuery?

Vanilla JS helped the developers in creating dynamic websites. Then came jQuery, a library of tools created by developers around the world, using Javascript. In simple words, jQuery is a lightweight and easy to use JavaScript library that helps in creating complex functionalities with few lines of coding.

What does $() mean in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.


2 Answers

A jQuery results set is an array like object that in general holds DOMElement, but jQuery does not really care about what type the objects in the result set have. Neither the DOMElements nor any other element that is stored within the jQuery result set is somehow mocked/wrapped, they are directly stored in the result set. jQuery tries to figure out what it has to do to those objects by looking at their available functions.

When you call .attr, jQuery checks for each object in the set if it has the function getAttribute if this is the case it assumes that it also has a function setAttribute.

If it does not have a function getAttribute, then it will forward the function call to the .prop() function, and prop will internally just do:

elem[name] = value 

So if you pass a plain object to jQuery, then it will just set its property.

var a = {    }    $(a).attr('test', 'hello world');    console.dir(a) // for `a`  the property `test` is set
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If you add a getAttribute and setAttribute function to that object then jQuery will call those:

var a = {    getAttribute() {      },    setAttribute() {      console.log('setAttribute was called')    }  }    $(a).attr('test', 'hello world');    console.dir(a);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

However, you should always assume, that the way and order how jQuery does these tests might change. Moreover, only rely on it if it is mentioned explicitly in the documentation.

like image 62
t.niese Avatar answered Sep 17 '22 19:09

t.niese


I believe you're incorrect about $(document) not referring to document, thus, the answer is (quite simply):

document['key']  = 'value' 

E.g. In Chrome:

> $(document)[0] === document true  > $(document).attr('blarg', 'narf') n.fn.init [document, context: document]  > document.blarg "narf"  > document.foo = 'bar' "bar"  > document.foo "bar" 
like image 22
broofa Avatar answered Sep 17 '22 19:09

broofa