Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
Drahcir Avatar asked Sep 13 '13 10:09

Drahcir


People also ask

What is getElementById in jQuery?

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.

How does the method in jQuery differs from the document getElementById () method?

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.

Which is fast document getElementById (' txtName ') or #txtName ')?

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.

What can I use instead of document getElementById?

A commonly-used alternative to document. getElementById is using a jQuery selector which you read about more here.


2 Answers

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

like image 64
c.P.u1 Avatar answered Sep 29 '22 11:09

c.P.u1


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.

like image 31
Prisoner Avatar answered Sep 29 '22 12:09

Prisoner