What is the difference between JavaScript’s getElementById() and getElementsByName() functions?

<input type="text" name="foo" id="bar">
                   ^^^^       ^^

getElementsByName gets elements by their name, getElementById gets the element by its id. There may be many elements on a page with the same name (hence getElementsByName always returns a list of elements), but there is (must) only be one element with a given id (therefore getElementById only returns a single element).

Leave a Comment