One other difference: getElementById
returns the first match, while $('#...')
returns a collection of matches - yes, the same ID can be repeated in an HTML doc.
Further, getElementId
is called from the document, while $('#...')
can be called from a selector. So, in the code below, document.getElementById('content')
will return the entire body but $('form #content')[0]
will return inside of the form.
<body id="content">
<h1>Header!</h1>
<form>
<div id="content"> My Form </div>
</form>
</body>
It might seem odd to use duplicate IDs, but if you are using something like Wordpress, a template or plugin might use the same id as you use in the content. The selectivity of jQuery could help you out there.