--- title: Document.getElementsByName() slug: Web/API/Document/getElementsByName translation_of: Web/API/Document/getElementsByName ---
{{APIRef("DOM")}}

Метод getElementsByName()  объекта {{domxref("Document")}} возвращает коллекцию {{domxref("NodeList")}} элементов с заданным  {{domxref("element.name","name")}}.

Синтаксис

var elements = document.getElementsByName(name);

Пример

<!DOCTYPE html>
<html lang="en">
<title>Example: using document.getElementsByName</title>

<input type="hidden" name="up">
<input type="hidden" name="down">

<script>
  var up_names = document.getElementsByName("up");
  console.log(up_names[0].tagName); // displays "INPUT"
</script>
</html>

Notes

The {{domxref("element.name","name")}} attribute can only be applied in (X)HTML documents.

The returned {{domxref("NodeList")}} Collection contains all elements with the given name, such as {{htmlelement("meta")}}, {{htmlelement("object")}}, and even elements which do not support the name attribute at all.

The getElementsByName method works differently in IE10 and below. There, getElementsByName() also returns elements that have an id attribute with the specified value. Be careful not to use the same string as both a name and an id.

The getElementsByName method works differently in IE. There, getElementsByName() does not return all elements which may not have a name attribute (such as <span>).

Both IE and Edge return an {{domxref("HTMLCollection")}}, not a {{domxref("NodeList")}}

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', '#dom-document-getelementsbyname', "Document.getElementsByName()")}} {{ Spec2('HTML WHATWG') }}  
{{SpecName("DOM2 HTML", "html.html#ID-71555259", "Document.getElementsByName()")}} {{Spec2("DOM2 HTML")}} Initial definition

Browser compatibility

{{Compat("api.Document.getElementsByName")}}

See also