| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
The createElementNS() method of the Document interface creates a new element with the specified namespace URI and qualified name.
This is useful in mixed-namespace documents, such as SVG or MathML embedded in HTML, where the parser cannot reliably infer the namespace.
The createElement() method is simpler if you want to create a plain HTML element.
createElementNS(namespaceURI, qualifiedName)
createElementNS(namespaceURI, qualifiedName, options)
namespaceURIA string that specifies the namespaceURI to associate with the element. Some important namespace URIs are:
qualifiedNameA string containing the qualified name of the new element.
The nodeName property of the created element is initialized with this value.
The format of the qualified name is prefix:localName or localName, where the parts are defined as:
prefix OptionalA "short alias" for the namespace.
The prefix is optional, but if it is specified the namespaceURI parameter must also be specified.
If the prefix is set to xml or xmlns, the namespaceURI must be set to http://www.w3.org/XML/1998/namespace or http://www.w3.org/2000/xmlns/, respectively.
The value is used to initialize the new element's prefix property.
Defaults to null.
localNameThe local name of the element.
The value is used to initialize the new element's localName property.
options OptionalAn object with the following optional properties (note that only one of is and customElementRegistry may be set):
is OptionalA string defining the tag name for a custom element previously defined using customElements.define().
The new element will be given an is attribute whose value is the custom element's tag name.
customElementRegistry OptionalA CustomElementRegistry that sets the Scoped custom element registry of a custom element.
For backward compatibility, some browsers allow you to pass a string here instead of an object, where the string's value is the custom element's tag name. See Extending native HTML elements for more information on how to use this parameter.
The new Element.
NamespaceError DOMExceptionThrown if the namespaceURI value is:
prefix has a value.http://www.w3.org/XML/1998/namespace or http://www.w3.org/2000/xmlns/ when prefix is set to xml or xmlns, respectively.InvalidCharacterError DOMExceptionThrown if either the prefix or localName is not valid:
prefix must have at least one character, and cannot contain ASCII whitespace, NULL, /, or > (U+0000, U+002F, or U+003E, respectively).localName is a valid element name if it has a length of at least 1 and:
NULL, /, or > (U+0000, U+002F, or U+003E, respectively).: (U+003A), _ (U+005F), or any characters in the range U+0080 to U+10FFFF (inclusive), and the remaining code points only include those same characters along with the ASCII alphanumeric characters, - (U+002D), and . (U+002E),Note:
Earlier versions of the specification were more restrictive, requiring that the qualifiedName be a valid XML name.
NotSupportedError DOMExceptionThrown if both the is and customElementRegistry options are specified.
This shows how to create a new <div> element in the XHTML namespace.
const divElementXHTML = document.createElementNS(
"http://www.w3.org/1999/xhtml",
"div",
);
// This is equivalent!
const divElementHTML = document.createElement("div");
This example shows how you might create an SVG element (SVGSVGElement) and append it to the HTML <body> element.
Using createElementNS() with the SVG namespace is necessary when working with an HTML document.
If you were to call createElement("svg"), an HTMLUnknownElement would be returned, and the SVG would not be rendered.
const svgNS = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(svgNS, "svg");
svg.setAttribute("width", "100");
svg.setAttribute("height", "100");
const circle = document.createElementNS(svgNS, "circle");
circle.setAttribute("cx", "50");
circle.setAttribute("cy", "50");
circle.setAttribute("r", "40");
circle.setAttribute("fill", "steelblue");
svg.appendChild(circle);
document.body.appendChild(svg);
| Specification |
|---|
| DOM # ref-for-dom-document-createelementns |
This page was last modified on Apr 6, 2026 by MDN contributors.
DocumentactiveElementactiveViewTransitionadoptedStyleSheetsalinkColorallanchorsappletsbgColorbodycharacterSetchildElementCountchildrencompatModecontentTypecookiecurrentScriptcustomElementRegistrydefaultViewdesignModedirdoctypedocumentElementdocumentURIdomainembedsfeaturePolicyfgColorfirstElementChildfontsformsfragmentDirectivefullscreenfullscreenElementfullscreenEnabledheadhiddenimagesimplementationlastElementChildlastModifiedlastStyleSheetSetlinkColorlinkslocationpictureInPictureElementpictureInPictureEnabledpluginspointerLockElementpreferredStyleSheetSetprerenderingreadyStatereferrerrootElementscriptsscrollingElementselectedStyleSheetSetstyleSheetsstyleSheetSetstimelinetitleURLvisibilityStatevlinkColorxmlEncodingxmlVersionadoptNode()append()ariaNotify()browsingTopics()caretPositionFromPoint()caretRangeFromPoint()clear()close()createAttribute()createAttributeNS()createCDATASection()createComment()createDocumentFragment()createElement()createElementNS()createEvent()createExpression()createNodeIterator()createNSResolver()createProcessingInstruction()createRange()createTextNode()createTouch()createTouchList()createTreeWalker()elementFromPoint()elementsFromPoint()enableStyleSheetsForSet()evaluate()execCommand()exitFullscreen()exitPictureInPicture()exitPointerLock()getAnimations()getElementById()getElementsByClassName()getElementsByName()getElementsByTagName()getElementsByTagNameNS()getSelection()hasFocus()hasPrivateToken()hasRedemptionRecord()hasStorageAccess()hasUnpartitionedCookieAccess()importNode()moveBefore()mozSetImageElement()open()prepend()queryCommandEnabled()queryCommandState()queryCommandSupported()querySelector()querySelectorAll()releaseCapture()replaceChildren()requestStorageAccess()requestStorageAccessFor()startViewTransition()write()writeln()AbortControllerAbortSignalAbstractRangeAttrCDATASectionCharacterDataCommentCustomEventDOMErrorDOMExceptionDOMImplementationDOMParserDOMTokenListDocumentFragmentDocumentTypeElementEventEventTargetHTMLCollectionMutationObserverMutationRecordNamedNodeMapNodeNodeIteratorNodeListProcessingInstructionQuotaExceededErrorRangeShadowRootStaticRangeTextTreeWalkerXMLDocumentXPathEvaluatorXPathExpressionXPathResultXSLTProcessorYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |