/

HTML / CSS / JavaScript Tutorial

JavaScript Document own method: createAttribute()

[this page | pdf | back links]

The createAttribute() method (when applied to the document object of the JavaScript DOM) creates an attribute node.

 

It has the following syntax with the following parameters. It returns a node object represents the created attribute.

 

document.createAttribute(attributename)

 

Parameter

Required / Optional

Description

attributename

Required

An Attr object which is the name of the attribute to create

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
Created using JavaScript:<br><br>
<span id="element"></span>

<script>
var x = document.createElement("INPUT");
var att = document.createAttribute("value");
att.value = "opening value";
x.setAttributeNode(att);
document.getElementById("element").appendChild(x);
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodDomCreateAttribute() {
  return !!document.createAttribute;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile