Net-informations.com

document.getElementbyId( "myId") Vs. $("#myId)

The document.getElementbyId( "myId") is faster because its direct call to JavaScript engine. jQuery is a wrapper that normalizes DOM manipulation in a way that works consistently in every major browser. The $, however, builds a jQuery object . 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 document.getElementbyId( "myId") will return you a DOM object whereas $("#myId) will return you a jquery object. By using this jquery object you will be able to use jquery methods. Pure Javascript to access the DOM can be faster as you can cut the overhead that jQuery has on this. However it doesn't always have to be faster as you could write some major mistakes that slow things down again.

Generally you should follow the axiom of using the simplest code that gets the job done with appropriate efficiency. That would likely be $("#myId") unless you were really trying to optimize pure performance in which case you would use document.getElementById("myId") .










net-informations.com (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About