I've got a little problem with my JavaScript. I'm trying to learn how to change the attribute on a page using setAttribute(name, value), and nothing happens.
This is my test site's HTML code:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8" />
<title>Test</title>
</head>
<body>
<div id="test" class="time"></div>
<script src="js/script.js"></script>
</body>
</html>
This is my JavaScript code:
if(document.getElementById("test").hasAttribute("class")) {
alert("got message");
var test = "test";
document.getElementById("test").setAttribute("class", test);
}
It's very simple, so it should work, but only an alert pops up, and when I check the source of page, nothing changes. To be honest - I tried several different approaches and nothing worked. It must be something really, really stupid but I can't find it.
document.getElementById("test").className = test.attr()(which is undefined), notsetAttribute(). When this part is fixed, the code works. Whatever might be happening, there is no information that could be used to track down the causes. Please delete this and post a new question when you have demonstrateable problem.