This is going to be a really short one.
I'm just wondering what type I should use for jQuery elements?
Without jQuery I go on like this:
export class Modal {
constructor(protected element:HTMLElement) {
}
}
But, lets say element
will be a jQuery selector like $('.myDiv')
for example. What type should element
then have?
The typeof operator when applied to the jQuery object returns the string "function" . Basically that does mean that jQuery is a function. But the typing sort of stops there.
A jQuery object is array-like which means that it contains zero or more indexes (properties which names are positive integers starting with zero). Besides those indexes, a jQuery object contains these properties: length. context. selector.
getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object.
After installing the types needed (npm install --save-dev @types/jquery
), you can type it as just JQuery
:
constructor(protected element: JQuery) {
}
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