diff --git a/src/ui/popup.js b/src/ui/popup.js index 509de7b6532..8636862dc2f 100644 --- a/src/ui/popup.js +++ b/src/ui/popup.js @@ -411,7 +411,17 @@ export default class Popup extends Evented { * .addTo(map); */ setDOMContent(htmlNode: Node) { - this._createContent(); + if (this._content) { + // Clear out children first. + while (this._content.hasChildNodes()) { + if (this._content.firstChild) { + this._content.removeChild(this._content.firstChild); + } + } + } else { + this._content = DOM.create('div', 'mapboxgl-popup-content', this._container); + } + // The close button should be the last tabbable element inside the popup for a good keyboard UX. this._content.appendChild(htmlNode); this._createCloseButton(); @@ -479,14 +489,6 @@ export default class Popup extends Evented { } } - _createContent() { - if (this._content) { - DOM.remove(this._content); - } - - this._content = DOM.create('div', 'mapboxgl-popup-content', this._container); - } - _createCloseButton() { if (this.options.closeButton) { this._closeButton = DOM.create('button', 'mapboxgl-popup-close-button', this._content);