Can someone help me? I would like to do something like this on a textarea to set the maxlength attribute:
<!DOCTYPE html>
<html>
<head>
<style>
.democlass {
color: red;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<p>Click the button to create a "class" attribute with the value "democlass" and insert it to the H1 element above.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var h1 = document.getElementsByTagName("H1")[0];
var att = document.createAttribute("class");
att.value = "democlass";
h1.setAttributeNode(att);
}
</script>
</body>
</html>
My code is:
<!DOCTYPE html>
<html>
<head>
<style>
</head>
<body>
<textarea>Hello World</textarea>
<button onclick="myFunction()">change max length</button>
<script>
function myFunction() {
var text = document.getElementsByTagName("textarea");
var att = document.createAttribute("maxlength");
att.value = "100";
text.setAttributeNode(att);
}
</script>
</body>
</html>
And if I run the script by clicking the button the console says:
Uncaught TypeError: h1.setAttribute is not a function.
Ps: i'm new at stackoverflow :)