Drahcir Drahcir - 1 month ago 11
Javascript Question

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

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

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