Element.append() - Référence Web API | MDN

  • Skip to main content
  • Skip to search

Cette page a été traduite à partir de l'anglais par la communauté. Vous pouvez contribuer en rejoignant la communauté francophone sur MDN Web Docs.

View in English Always switch to English

Element.append() Baseline Widely available

Cette fonctionnalité est bien établie et fonctionne sur de nombreux appareils et versions de navigateurs. Elle est disponible sur tous les navigateurs depuis avril 2018.

  • Learn more
  • See full compatibility
  • Report feedback

La méthode Element.append() ajoute un ensemble d'objets Node ou de chaînes de caractères après le dernier enfant d'Element. Les chaînes de caractères sont insérées comme des nœuds Text.

Voici les différences entre Element.append() et Node.appendChild() :

  • Element.append() permet d'ajouter des chaînes de caractères tandis que Node.appendChild() accepte uniquement les objets Node.
  • Element.append() n'a pas de valeur de retour alors que Node.appendChild() renvoie l'objet Node ajouté.
  • Element.append() permet d'ajouter plusieurs nœuds et chaînes de caractères tandis que Node.appendChild() ne permet d'ajouter qu'un seul nœud.

Dans cet article

  • Syntaxe
  • Exemples
  • Spécifications
  • Compatibilité des navigateurs
  • Voir aussi

Syntaxe

jsappend(param1); append(param1, param2); append(param1, param2, /* ... ,*/ paramN);

Paramètres

param1, …, paramN

Un ensemble d'objets Node ou de chaînes de caractères à insérer.

Exceptions

DOMException HierarchyRequestError

Levée lorsque le nœud ne peut être inséré à l'emplacement indiqué dans la hiérarchie.

Exemples

Ajouter un élément

jslet div = document.createElement("div"); let p = document.createElement("p"); div.append(p); console.log(div.childNodes); // NodeList [ <p> ]

Ajouter du texte

jslet div = document.createElement("div"); div.append("Du texte"); console.log(div.textContent); // "Du texte"

Ajouter un élément et du texte

jslet div = document.createElement("div"); let p = document.createElement("p"); div.append("Du texte", p); console.log(div.childNodes); // NodeList [ #text "Du texte", <p> ]

append() est hors de la portée créée par with

La méthode append() n'est pas disponible dans la portée créée par une instruction with. Voir Symbol.unscopables pour plus d'informations.

jslet div = document.createElement("div"); with (div) { append("toto"); } // ReferenceError: append is not defined

Spécifications

Specification
DOM# ref-for-dom-parentnode-append①

Compatibilité des navigateurs

Voir aussi

  • Element.prepend()
  • Node.appendChild()
  • Element.after()
  • Element.insertAdjacentElement()
  • NodeList

Help improve MDN

Cette page vous a-t-elle été utile ? Yes No Learn how to contribute

Cette page a été modifiée le 3 août 2023 par les contributeurs du MDN.

View this page on GitHub • Report a problem with this content Filter sidebar
  1. Référence du DOM
  2. Element
  3. Propriétés d'instance
    1. ariaActiveDescendantElement
    2. ariaAtomic
    3. ariaAutoComplete
    4. ariaBrailleLabel
    5. ariaBrailleRoleDescription
    6. ariaBusy
    7. ariaChecked
    8. ariaColCount
    9. ariaColIndex
    10. ariaColIndexText
    11. ariaColSpan
    12. ariaControlsElements
    13. ariaCurrent
    14. ariaDescribedByElements
    15. ariaDescription
    16. ariaDetailsElements
    17. ariaDisabled
    18. ariaErrorMessageElements
    19. ariaExpanded
    20. ariaFlowToElements
    21. ariaHasPopup
    22. ariaHidden
    23. ariaInvalid
    24. ariaKeyShortcuts
    25. ariaLabel
    26. ariaLabelledByElements
    27. ariaLevel
    28. ariaLive
    29. ariaModal
    30. ariaMultiLine
    31. ariaMultiSelectable
    32. ariaOrientation
    33. ariaOwnsElements
    34. ariaPlaceholder
    35. ariaPosInSet
    36. ariaPressed
    37. ariaReadOnly
    38. ariaRelevant Non standard
    39. ariaRequired
    40. ariaRoleDescription
    41. ariaRowCount
    42. ariaRowIndex
    43. ariaRowIndexText
    44. ariaRowSpan
    45. ariaSelected
    46. ariaSetSize
    47. ariaSort
    48. ariaValueMax
    49. ariaValueMin
    50. ariaValueNow
    51. ariaValueText
    52. assignedSlot
    53. attributes
    54. childElementCount
    55. children
    56. classList
    57. className
    58. clientHeight
    59. clientLeft
    60. clientTop
    61. clientWidth
    62. currentCSSZoom
    63. customElementRegistry
    64. elementTiming Expérimental
    65. firstElementChild
    66. id
    67. innerHTML
    68. lastElementChild
    69. localName
    70. namespaceURI
    71. nextElementSibling
    72. outerHTML
    73. part
    74. prefix
    75. previousElementSibling
    76. role
    77. scrollHeight
    78. scrollLeft
    79. scrollLeftMax Non standard
    80. scrollTop
    81. scrollTopMax Non standard
    82. scrollWidth
    83. shadowRoot
    84. slot
    85. tagName
  4. Méthodes d'instance
    1. after()
    2. animate()
    3. append()
    4. ariaNotify() Expérimental Non standard
    5. attachShadow()
    6. before()
    7. checkVisibility()
    8. closest()
    9. computedStyleMap()
    10. getAnimations()
    11. getAttribute()
    12. getAttributeNames()
    13. getAttributeNode()
    14. getAttributeNodeNS()
    15. getAttributeNS()
    16. getBoundingClientRect()
    17. getClientRects()
    18. getElementsByClassName()
    19. getElementsByTagName()
    20. getElementsByTagNameNS()
    21. getHTML()
    22. hasAttribute()
    23. hasAttributeNS()
    24. hasAttributes()
    25. hasPointerCapture()
    26. insertAdjacentElement()
    27. insertAdjacentHTML()
    28. insertAdjacentText()
    29. matches()
    30. moveBefore()
    31. prepend()
    32. querySelector()
    33. querySelectorAll()
    34. releasePointerCapture()
    35. remove()
    36. removeAttribute()
    37. removeAttributeNode()
    38. removeAttributeNS()
    39. replaceChildren()
    40. replaceWith()
    41. requestFullscreen()
    42. requestPointerLock()
    43. scroll()
    44. scrollBy()
    45. scrollIntoView()
    46. scrollIntoViewIfNeeded() Non standard
    47. scrollTo()
    48. setAttribute()
    49. setAttributeNode()
    50. setAttributeNodeNS()
    51. setAttributeNS()
    52. setCapture() Non standard Obsolète
    53. setHTML()
    54. setHTMLUnsafe()
    55. setPointerCapture()
    56. toggleAttribute()
  5. Évènements
    1. afterscriptexecute Non standard Obsolète
    2. animationcancel
    3. animationend
    4. animationiteration
    5. animationstart
    6. auxclick
    7. beforeinput
    8. beforematch
    9. beforescriptexecute Non standard Obsolète
    10. beforexrselect Expérimental
    11. blur
    12. click
    13. compositionend
    14. compositionstart
    15. compositionupdate
    16. contentvisibilityautostatechange
    17. contextmenu
    18. copy
    19. cut
    20. dblclick
    21. DOMActivate Obsolète
    22. DOMMouseScroll Non standard Obsolète
    23. focus
    24. focusin
    25. focusout
    26. fullscreenchange
    27. fullscreenerror
    28. gesturechange Non standard
    29. gestureend Non standard
    30. gesturestart Non standard
    31. gotpointercapture
    32. input
    33. keydown
    34. keypress Obsolète
    35. keyup
    36. lostpointercapture
    37. mousedown
    38. mouseenter
    39. mouseleave
    40. mousemove
    41. mouseout
    42. mouseover
    43. mouseup
    44. mousewheel Non standard Obsolète
    45. MozMousePixelScroll Non standard Obsolète
    46. paste
    47. pointercancel
    48. pointerdown
    49. pointerenter
    50. pointerleave
    51. pointermove
    52. pointerout
    53. pointerover
    54. pointerrawupdate
    55. pointerup
    56. scroll
    57. scrollend
    58. scrollsnapchange Expérimental
    59. scrollsnapchanging Expérimental
    60. securitypolicyviolation
    61. touchcancel
    62. touchend
    63. touchmove
    64. touchstart
    65. transitioncancel
    66. transitionend
    67. transitionrun
    68. transitionstart
    69. webkitmouseforcechanged Non standard
    70. webkitmouseforcedown Non standard
    71. webkitmouseforceup Non standard
    72. webkitmouseforcewillbegin Non standard
    73. wheel
  6. Héritage
    1. Node
    2. EventTarget
  7. Pages liées à DOM
    1. AbortController
    2. AbortSignal
    3. AbstractRange
    4. Attr
    5. CDATASection
    6. CharacterData
    7. Comment
    8. CustomEvent
    9. DOMError Obsolète
    10. DOMException
    11. DOMImplementation
    12. DOMParser
    13. DOMTokenList
    14. Document
    15. DocumentFragment
    16. DocumentType
    17. Event
    18. EventTarget
    19. HTMLCollection
    20. MutationObserver
    21. MutationRecord
    22. NamedNodeMap
    23. Node
    24. NodeIterator
    25. NodeList
    26. ProcessingInstruction
    27. QuotaExceededError Expérimental
    28. Range
    29. ShadowRoot
    30. StaticRange
    31. Text
    32. TreeWalker
    33. XMLDocument
    34. XPathEvaluator
    35. XPathExpression
    36. XPathResult
    37. XSLTProcessor
  8. Guides
    1. Anatomy of the DOM
    2. Réflexion des attributs
    3. Sélection et parcours de l'arbre DOM
    4. Construire et mettre à jour l'arbre DOM
    5. Travailler avec des événements

Tag » Add Element Dom Js