BBC's guide to development
  • General

    • About
    • Tools
    • Git(hub)
    • Showpad
    • Hosting
    • Maintenance
    • Security
    • Go live checklist
  • Front-end development

    • Bundlers
    • CSS/SCSS
    • Javascript
    • Vue
    • PHP
    • Mails
    • Dev Faq
  • Functions
  • Mixins
  • General

    • OOP Structure
  • Component Classes

    • Accordion
    • App
    • Component
    • HighwayApp
    • Popup
    • PNG Sequencer
    • Tab
  • Manager Classes

    • BountListenerMgr
    • Cache
    • Configuration
    • InViewStateMgr
    • Instance Manager
    • Event dispatcher
  • Factories

    • SwiperFactory
  • PDF

    • AssetLoader
    • BasePdfDoc
    • TemplatePdfDoc
    • CustomPdfDoc
  • Utility functions

    • canvas
    • Connection Status
    • css
    • dev
    • placeholder
    • dom
    • fetch
    • json
    • object
    • scroll
    • scrollbar
    • spreadsheets
    • string
    • url
  • General

    • ComponentMgr
    • ThreeJsViewer
  • Components

    • ComponentMgr
    • GltfModel
    • Snappable
    • Socket
    • ThreeJsViewer
    • ThreeJsViewerCamera
  • Loaders

    • ConfigurationSerializer
    • GltfBlockParser
  • Utils

    • CanvasInputAdapter
    • CollisionManager
    • SocketGridExpander
    • blender
    • headless
  • General

    • Troubleshooting
    • Legacy
  • Components

    • AssetBar
    • ConfigGenerator
    • ShowpadApp
  • Managers

    • Assets
    • AppsDb
    • Config
  • Utils

    • Connection Status
    • general
    • showpad-interactive
    • showpad-upload
  • Components

    • Accordion
    • BackButton
    • Breadcrumb
    • ByltButton
    • Hamburger
    • Icon
    • Logo
    • Loader
    • Modal
    • Popup
    • Prompt
    • ProgressBar
    • TextLoader
  • Composables

    • useDebugMode
    • useConnectionStatus
  • Utils

    • dom
    • props
  • General

    • General
    • Tracking
  • Components

    • Accordion
    • ActionButton
    • AssetItem
    • AssetList
    • BackButton
    • ConfigGenButton
    • Logo
    • Media
    • Modal
    • Popup
    • Prompt
    • SPButton
    • SPRouterView
    • SPTrackedRouterLink
    • TextLoader
    • View
  • Composables

    • useConnectionStatus
  • Stores

    • useAppsDbStore
    • useBreadcrumbStore
    • useShowpadAPIStore
    • useShowpadSDKStore
    • useSpConfigStore
    • useSpStore
    • useSpTrackingStore
  • The New Kit

    • General
    • Installation & Usage
    • ACF Blocks
    • PHPCS
    • Functions
    • Vite
    • WP Config
    • Staging Deployment
  • Best Practices

    • Page Structure
    • Fonts/Typography
  • Todo
GitHub
  • General

    • About
    • Tools
    • Git(hub)
    • Showpad
    • Hosting
    • Maintenance
    • Security
    • Go live checklist
  • Front-end development

    • Bundlers
    • CSS/SCSS
    • Javascript
    • Vue
    • PHP
    • Mails
    • Dev Faq
  • Functions
  • Mixins
  • General

    • OOP Structure
  • Component Classes

    • Accordion
    • App
    • Component
    • HighwayApp
    • Popup
    • PNG Sequencer
    • Tab
  • Manager Classes

    • BountListenerMgr
    • Cache
    • Configuration
    • InViewStateMgr
    • Instance Manager
    • Event dispatcher
  • Factories

    • SwiperFactory
  • PDF

    • AssetLoader
    • BasePdfDoc
    • TemplatePdfDoc
    • CustomPdfDoc
  • Utility functions

    • canvas
    • Connection Status
    • css
    • dev
    • placeholder
    • dom
    • fetch
    • json
    • object
    • scroll
    • scrollbar
    • spreadsheets
    • string
    • url
  • General

    • ComponentMgr
    • ThreeJsViewer
  • Components

    • ComponentMgr
    • GltfModel
    • Snappable
    • Socket
    • ThreeJsViewer
    • ThreeJsViewerCamera
  • Loaders

    • ConfigurationSerializer
    • GltfBlockParser
  • Utils

    • CanvasInputAdapter
    • CollisionManager
    • SocketGridExpander
    • blender
    • headless
  • General

    • Troubleshooting
    • Legacy
  • Components

    • AssetBar
    • ConfigGenerator
    • ShowpadApp
  • Managers

    • Assets
    • AppsDb
    • Config
  • Utils

    • Connection Status
    • general
    • showpad-interactive
    • showpad-upload
  • Components

    • Accordion
    • BackButton
    • Breadcrumb
    • ByltButton
    • Hamburger
    • Icon
    • Logo
    • Loader
    • Modal
    • Popup
    • Prompt
    • ProgressBar
    • TextLoader
  • Composables

    • useDebugMode
    • useConnectionStatus
  • Utils

    • dom
    • props
  • General

    • General
    • Tracking
  • Components

    • Accordion
    • ActionButton
    • AssetItem
    • AssetList
    • BackButton
    • ConfigGenButton
    • Logo
    • Media
    • Modal
    • Popup
    • Prompt
    • SPButton
    • SPRouterView
    • SPTrackedRouterLink
    • TextLoader
    • View
  • Composables

    • useConnectionStatus
  • Stores

    • useAppsDbStore
    • useBreadcrumbStore
    • useShowpadAPIStore
    • useShowpadSDKStore
    • useSpConfigStore
    • useSpStore
    • useSpTrackingStore
  • The New Kit

    • General
    • Installation & Usage
    • ACF Blocks
    • PHPCS
    • Functions
    • Vite
    • WP Config
    • Staging Deployment
  • Best Practices

    • Page Structure
    • Fonts/Typography
  • Todo
GitHub
  • Dom utilities

Dom utilities

import { containsHtml, isHtml, spanify, strToDom, splitText, wrapEl, downloadToDevice, removeWithTrace, restoreFromTrace } from '@bbc/front-end-kit/js/utils/dom';

This file contains all DOM-related utility functions that help shorten the codebase and speed up development.

containsHtml

Detects if a given string contains HTML tags or entities.

Parameters
  • str (string): The string to check.
Returns

boolean

import { containsHtml } from '@bbc/front-end-kit/js/utils/dom';

containsHtml('Lorem Ipsum'); // false
containsHtml('Lorem <strong>Ipsum</strong>'); // true
containsHtml('<div>Lorem <strong>Ipsum</strong></div>'); // true

isHtml

Detects if a given string is exclusively an HTML tag or entity — no surrounding text.

Parameters
  • str (string): The string to check.
Returns

boolean

import { isHtml } from '@bbc/front-end-kit/js/utils/dom';

isHtml('Lorem Ipsum'); // false
isHtml('Lorem <strong>Ipsum</strong>'); // false
isHtml('<div>Lorem <strong>Ipsum</strong></div>'); // true

spanify

Wraps a string in a <span> element with the given attributes. Attribute keys are converted to kebab-case; object values are JSON-stringified.

Parameters

  • str (string): The content to place inside the span.
  • attributes (object, optional): Key-value pairs to set as attributes on the span.

Returns

string — an HTML string of the span element.

import { spanify } from '@bbc/front-end-kit/js/utils/dom';

spanify('Hello', { className: 'highlight', dataIndex: 0 });
// '<span class-name="highlight" data-index="0">Hello</span>'

strToDom

Converts an HTML string into a live DOM element without adding it to the page first.

Parameters

  • str (string): An HTML string to parse.

Returns

Element — the first element child of the parsed fragment.

javascript
import { strToDom } from '@bbc/front-end-kit/js/utils/dom';

const $wrapper = strToDom('<div class="element"></div>');
const $inner = strToDom(`<div class="element__inner" id="el-${Math.round(Math.random() * 100)}"></div>`);

$wrapper.classList.add('element--modifier');
$wrapper.appendChild($inner);
document.body.appendChild($wrapper);
html
<body>
    <div class="element element--modifier">
        <div class="element__inner"></div>
    </div>
</body>

splitText

Splits all text within the target element(s) into individual <span> elements per word and per character.

Parameters

  • target (string | NodeList | Element): A CSS selector string, NodeList, or single Element to process.
  • attributes (object, optional): Attribute objects applied to word and character spans.
    • attributes.word: Attributes for each word-level span.
    • attributes.character: Attributes for each character-level span.

Returns

undefined

import { splitText } from '@bbc/front-end-kit/js/utils/dom';

splitText('.my-text', {
    word: { class: 'word' },
    character: { class: 'char' }
});

wrapEl

Wraps a given element in a newly created <div>.

Parameters

  • $el (Element): The element to wrap.
  • options (object, optional):
    • replace (boolean, default false): If true, attempts to insert the wrapper in place of $el in the DOM.

Returns

Element — the original $el (not the wrapper div).

Warning

The replace: true branch calls parentNode.insertAfter(), which does not exist on Node. This option is currently broken and will throw a TypeError. Omit replace or handle placement manually.

import { wrapEl } from '@bbc/front-end-kit/js/utils/dom';

const $el = document.querySelector('.my-element');

// wraps $el in a div and returns $el
const $el = wrapEl($el);

downloadToDevice

Triggers a file download in the browser by creating a temporary anchor and clicking it programmatically.

Parameters

  • url (string): The URL of the file to download.
  • filename (string): The filename the browser should use when saving.

Returns

undefined

import { downloadToDevice } from '@bbc/front-end-kit/js/utils/dom';

downloadToDevice('https://example.com/file.pdf', 'report.pdf');

removeWithTrace

Removes an element from the DOM but leaves a hidden <span> placeholder in its place. The placeholder's ID is recorded in the element's data-origins attribute so it can be restored later with restoreFromTrace.

Multiple calls on the same element append IDs to data-origins separated by |, creating a position history.

Parameters

  • $el (Element): The element to remove.

Returns

Element — the removed element (with updated data-origins).

import { removeWithTrace } from '@bbc/front-end-kit/js/utils/dom';

const $el = document.querySelector('.my-element');
removeWithTrace($el); // $el removed, span placeholder left behind

Tips

Very handy for elements that need to move between DOM positions across screen sizes. Use a single element instance that travels rather than duplicating markup.

restoreFromTrace

Moves an element back to its previous DOM position by finding the last placeholder ID in its data-origins attribute and replacing it with the element.

Parameters

  • $el (Element): The element to restore.

Returns

Element — the restored element (chainable).

import { removeWithTrace, restoreFromTrace } from '@bbc/front-end-kit/js/utils/dom';

const $el = document.querySelector('.my-element');

removeWithTrace($el);
document.querySelector('.popup').appendChild($el);
removeWithTrace($el);
document.querySelector('.other').appendChild($el);

restoreFromTrace($el); // back to .popup
restoreFromTrace($el); // back to original position

// or chained:
restoreFromTrace(restoreFromTrace($el));
Edit this page
Last Updated: 4/27/26, 12:56 PM
Contributors: Nicolas Jaenen