What is the JSDoc type for document.getElementById('myID') and a jQuery Element?

Drahcir picture Drahcir · Sep 13, 2013 · Viewed 10.9k times · Source

I'm trying to document my functions with JSDoc syntax.

/**
 * 
 * My Description
 * 
 * @param {JQuery|???} input
 * @returns {JQuery}
 */
function foo(input){
    return $('selector');
}

The above function accepts a single argument which can be either a JQuery object, or an element returned by document.getElementById.

What is the valid JSDoc type for the return value of getElementById?

For example, the following are both valid:

foo($('#input'));
foo(document.getElementById('input'));

Also, where can I find this out in future?

Answer

c.P.u1 picture c.P.u1 · Sep 13, 2013

getElementById will always return a subtype of Element. In the case of an HTML document, HTMLElement will be more appropriate

document.getElementById('some-anchor').constructor //HTMLAnchorElement
document.getElementById('some-div').constructor //HTMLDivElement

In all cases, document.getElementById('some-element') instanceof HTMLElement will, IMHO, return true