Select an element by ID
jQuery $('#foo')
2015 page
This is the original page from 2015, kept at its address while the tested rewrite is in progress. The code is of its time: it ran in Internet Explorer and does not use what browsers have gained since. The rewrite replaces it here.
In short
getElementById – a fast and cross browser safe way for selecting DOM elements.
The method getElementById() is supported in all major browsers and returns the first element with the specified ID. It is the fastest way for accessing DOM tree elements. Example:
var el = document.getElementById('foo');
el.innerHTML = 'Hello World!';The method is blazingly fast, but due to the limitation of selecting elements only by id, its usefulness is limited. The linked reference shows a performance comparison between getElementById() and querySelectorAll(). The latter is a convenient method for accessing DOM elements by almost arbitrary CSS selectors.