I'm somewhat new to javascript, and have been using libraries/css frameworks to construct some of my sites that I build just for fun. To use certain functionalities provided by the framework creators, the documentation says to call a method on a jQuery object, like $('.my-class').someMethod()
. As an example, the following is how initialize Materialize's autocomplete function on any input field you want:
$(document).ready(function(){
$('input.autocomplete').autocomplete({
data: {
"Apple": null,
"Microsoft": null,
"Google": 'https://placehold.it/250x250'
},
});
});
Now, it is my understanding that when you run: $('input.autocomplete')
, jQuery will return a 'jQuery object', as stated here.
So I ran a test in my browser with a dummy page, and logged this object to the console, and it returned an array with one element -- that of the div that I searched for:
I'm guessing all of the purple text in the image above are properties of the jQuery object.
Now for my question: how is it possible for me to call .autocomplete()
on this object? No .autocomplete() method exists inside the jQuery object, so why would this not throw an error?
I'm having a hard time understanding how .autocomplete()
can be run on virtually any jQuery object... how does it know where to find this method? I thought to use dot notation, it needed to be a method within the class you are chaining it to.