<html>
<head>
<style>
.tagging {
border: 1px solid black;
width: 20px;
height: 30px;
}
</style>
<script>
window.onload = function() {
var div = document.getElementsByTagName("div");
div[0].class = "tagging";
}
</script>
</head>
<body>
<div></div>
</body>
</html>
This is my code. I wonder why it doesn't work when I assign class attribute via javascript, but it works when I assign inline directly in html
<div class="tagging"></div>
classis a reserved keyword in many languages, the creators of the DOM API decided to map theclassattribute to theclassNameproperty. In JavaScript it wouldn't actually matter because even thoughclassis a reserved keyword, you can use such keywords as property names.