Menu

gitpiper

createElement javascript Code Snippet in 2024

browserbeginner

Last Updated: 26 March 2024

Creates an element from a string (without appending it to the document). If the given string contains multiple elements, only the first one will be returned.

  • Use Document.createElement() to create a new element.
  • Use Element.innerHTML to set its inner HTML to the string supplied as the argument.
  • Use ParentNode.firstElementChild to return the element version of the string.
const createElement = str => { const el = document.createElement('div'); el.innerHTML = str; return el.firstElementChild; };
const el = createElement( `<div class="container"> <p>Hello!</p> </div>` ); console.log(el.className); // 'container'

javascript snippet similar to createElement For You in March 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️