aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/node/haschildnodes/index.md
blob: 54fd502016d15139fae1ab3dee64bae5c733aff0 (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
---
title: element.hasChildNodes
slug: Web/API/Node/hasChildNodes
tags:
  - API
  - DOM
  - Method
  - NeedsSpecTable
  - Node
  - Reference
translation_of: Web/API/Node/hasChildNodes
---
{{APIRef("DOM")}}

La méthode **`Node.hasChildNodes()`** renvoie un {{jsxref("Boolean")}} indiquant si le {{domxref("Node","noeud")}} actuel possède des [nœuds enfants](/fr/docs/Web/API/Node/childNodes) ou non.

## Syntaxe

```js
bool = node.hasChildNodes();
```

### Valeur de retour

Un {{jsxref("Boolean")}} qui est `true` si le nœud a des nœuds enfants, et `false` dans le cas contraire.

## Exemple

```js
let foo = document.getElementById('foo');

if (foo.hasChildNodes()) {
  // Faire quelque chose avec 'foo.childNodes'
}
```

## Prothèse d'émulation

```js
(function(prototype) {
  prototype.hasChildNodes = prototype.hasChildNodes || function() {
    return !!this.firstChild;
  }
})(Node.prototype);
```

Il y a différentes façons de déterminer si le noeud a un noeud enfant :

- `node.hasChildNodes()`
- `node.firstChild != null` (ou simplement `node.firstChild`)
- `node.childNodes && node.childNodes.length` (ou `node.childNodes.length > 0`)

## Spécification

| Spécification                                                                                        | Statut                           | Commentaire |
| ---------------------------------------------------------------------------------------------------- | -------------------------------- | ----------- |
| {{SpecName("DOM WHATWG", "#dom-node-haschildnodes", "Node: hasChildNodes")}} | {{Spec2("DOM WHATWG")}} |             |

## Compatibilité des navigateurs

{{Compat("api.Node.hasChildNodes")}}

## Voir aussi

- {{domxref("Node.childNodes")}}
- {{domxref("Node.hasAttributes")}}