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

Il metodo getElementsByName() dell'oggetto {{domxref("Document")}} ritorna una raccolta di elementi {{domxref("NodeList")}} con un determinato {{domxref("element.name","name")}} nel documento.

Sintassi

var elements = document.getElementsByName(name);

Esempio

<!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); // ritorna "INPUT"
</script>
</html>

Appunti

L'attributo {{domxref("element.name","name")}} può essere applicato solo nei documenti (X)HTML.

La raccolta {{domxref("NodeList")}} restituita contiene tutti gli elementi con il parametro name dato, come {{htmlelement("meta")}}, {{htmlelement("object")}}, e persino elementi che non supportano affatto l'attributo name.

Il metodo getElementsByName funziona in modo diverso in IE10 e versioni precedenti. Lì, getElementsByName() restituisce anche gli elementi che hanno un attributo id con il valore specificato. Fai attenzione a non usare la stessa stringa sia di un name che di un id.

Il metodo getElementsByName funziona in modo diverso in IE. Lì, getElementsByName() non restituisce tutti gli elementi che potrebbero non avere un attributo name (come <span>).

Sia IE che Edge restituiscono una {{domxref("HTMLCollection")}}, non una {{domxref("NodeList")}}

Specifiche

Specifica Stato Commento
{{SpecName('HTML WHATWG', '#dom-document-getelementsbyname', "Document.getElementsByName()")}} {{ Spec2('HTML WHATWG') }}  
{{SpecName("DOM2 HTML", "html.html#ID-71555259", "Document.getElementsByName()")}} {{Spec2("DOM2 HTML")}} Definizione iniziale

Compatibilità con i browser

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

Vedi anche