Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of a jQuery object in TypeScript?

Tags:

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?

like image 260
user2952238 Avatar asked Jun 17 '17 14:06

user2952238


People also ask

What is type in jQuery?

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.

What is jQuery object?

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.

Which method returns the element as a jQuery object?

getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object.


1 Answers

After installing the types needed (npm install --save-dev @types/jquery), you can type it as just JQuery:

constructor(protected element: JQuery) {

}
like image 66
Saravana Avatar answered Sep 23 '22 17:09

Saravana