aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/document/getelementsbyname/index.html
blob: c8136c8de758a051560b6b4617347c3a77bd76eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---
title: document.getElementsByName()
slug: Web/API/Document/getElementsByName
tags:
  - API
  - DOM
  - Document
  - HTML
  - Méthodes
translation_of: Web/API/Document/getElementsByName
---
<p>{{ ApiRef("DOM") }}</p>

<p>Renvoie une liste des éléments portant un {{domxref("element.name","name")}} donné dans le document (X)HTML.</p>

<h2 id="Syntaxe" name="Syntaxe">Syntaxe</h2>

<pre class="eval"><em>elements</em> = document.getElementsByName(<em>name</em>)
</pre>

<ul>
 <li><code>elements</code> est une collection de {{domxref("NodeList")}}</li>
 <li><code>name</code> est la valeur de l'attribut <code>name</code> des éléments.</li>
</ul>

<h2 id="Exemple" name="Exemple">Exemple</h2>

<pre class="brush:html line-numbers  language-html"><code class="language-html"><span class="doctype token">&lt;!DOCTYPE html&gt;</span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>html</span> <span class="attr-name token">lang</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>en<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>head</span><span class="punctuation token">&gt;</span></span>
 ...
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>head</span><span class="punctuation token">&gt;</span></span>

<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>body</span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>form</span> <span class="attr-name token">name</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>up<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span><span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>input</span> <span class="attr-name token">type</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>text<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span><span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>form</span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>div</span> <span class="attr-name token">name</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>down<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span><span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>input</span> <span class="attr-name token">type</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>text<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span><span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>div</span><span class="punctuation token">&gt;</span></span>

<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>script</span><span class="punctuation token">&gt;</span></span><span class="language-javascript script token">
<span class="keyword token">var</span> up_forms <span class="operator token">=</span> document<span class="punctuation token">.</span><span class="function token">getElementsByName</span><span class="punctuation token">(</span><span class="string token">"up"</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>up_forms<span class="punctuation token">[</span><span class="number token">0</span><span class="punctuation token">]</span><span class="punctuation token">.</span>tagName<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// retourne "FORM"</span>
</span><span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>script</span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>body</span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>html</span><span class="punctuation token">&gt;</span></span></code></pre>

<h2 id="Notes" name="Notes">Notes</h2>

<p><span id="result_box" lang="fr"><span>L'attribut <code><a href="https://developer.mozilla.org/fr/docs/Web/API/Element/name">name</a></code> est uniquement applicable aux documents (X) HTML.</span> <span>La méthode renvoie une collection </span></span> {{domxref("NodeList")}} <span lang="fr"><span> en cours qui contient tous les éléments avec une valeur donnée pour l'attribut name, tels que </span></span> {{htmlelement("meta")}} <span lang="fr"><span> ou </span></span> {{htmlelement("object")}} <span lang="fr"><span> ou même si le nom est placé sur des éléments qui ne supportent pas du tout un attribut <code>name</code>.</span></span></p>

<p><span id="result_box" lang="fr"><span>La méthode <strong>getElementsByName</strong> fonctionne différemment dans différents navigateurs.</span> <span>Dans IE &lt;10, la méthode getElementsByName () renvoie également les éléments qui ont un attribut id avec la valeur spécifiée.</span> <span>Vous devriez donc faire attention à ne pas utiliser la même chaîne pour le nom et l'identifiant.</span></span></p>

<h2 id="Sp.C3.A9cification" name="Sp.C3.A9cification">Spécifications</h2>

<ul>
 <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259">DOM Level 2 HTML : getElementsByName</a><a class="external" href="http://www.yoyodesign.org/doc/w3c/dom2-html/html.html#ID-71555259">traduction en français</a> (non normative)</li>
 <li><a href="http://www.whatwg.org/html/#dom-document-getelementsbyname" title="http://www.whatwg.org/html/">HTML5 : getElementsByName</a></li>
</ul>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Spécification</th>
   <th scope="col">Statut</th>
   <th scope="col">Commentaire</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', '#dom-document-getelementsbyname', "Document.getElementsByName()")}}</td>
   <td>{{ Spec2('HTML WHATWG') }}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName("DOM2 HTML", "html.html#ID-71555259", "Document.getElementsByName()")}}</td>
   <td>{{Spec2("DOM2 HTML")}}</td>
   <td>Définition initiale</td>
  </tr>
 </tbody>
</table>

<h2 id="See_also" name="See_also">Voir aussi</h2>

<ul>
 <li>{{domxref("document.getElementById()")}} pour retourner une référence à un élément par son ID</li>
 <li>{{domxref("document.getElementsByTagName()")}} pour renvoyer les références sur les éléments avec la balise de nom donnée</li>
 <li>{{domxref("document.querySelector()")}} pour des sélecteurs par des requêtes comme <code>'div.myclass'</code></li>
</ul>

<p> </p>