JavaScript DOM HTML method: setAttribute()
[this page | pdf | back links]
The setAttribute() method (when applied to
HTML
elements in the JavaScript
DOM) sets the
specified attribute value.
It
has the following syntax with the following parameters. It does not return
a value.
element.setAttribute(attributename,
attributevalue)
Parameter
|
Required / Optional
|
Description
|
attributename
|
Required
|
String indicating name
of attribute to be set
|
attributevalue
|
Required
|
String indicating value
of that attribute
|
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
Created using HTML:<br>
<ol start="2">
<li>Apple</li>
</ol>
<br><br>Created using JavaScript:<br>
<span id="element"></span>
<script>
var y = document.getElementById("element");
var x = document.createElement("OL");
x.setAttribute("start","2");
var x1 = document.createElement("LI");
var txt1 = document.createTextNode("Apple");
x1.appendChild(txt1);
x.appendChild(x1);
x.appendChild(x2);
</script>
</body>
</html>
|
FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodDomHtmlSetAttribute() {
var z = document.createElement("TABLE"); return !!z.setAttribute;
} |
NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)