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?
getElementById() which is used to select the element by its id attribute. The getElementById() method returns the elements that has given ID which is passed to the function.
First, it has to parse the selector, since jQuery can find things by class, attribute, ancestor, etc. whereas document. getElementById only finds elements by their ID. The jQuery object is not a native object, so is slower to create, and it also has much more potential.
The simple answer is document. getElementByID('txtName') is faster than $('#txtName') because jQuery is written on top of javascript. It means internally jQuery is using only native javascript codes.
A commonly-used alternative to document. getElementById is using a jQuery selector which you read about more here.
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
Whilst technically the return value of getElementById
is object
, the documentation should help the developer to know what it is.
I'd personally go with Element
but there is no indications as to what you should use.
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