javascript - Can't change html attribute with external script -


i've got little problem javascript. i'm trying learn how change attribute on page using setattribute(name, value), , nothing happens.

this 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 javascript code:

if(document.getelementbyid("test").hasattribute("class")) {     alert("got message");     var test = "test";     document.getelementbyid("test").setattribute("class", test); } 

it's simple, should work, alert pops up, , when check source of page, nothing changes. honest - tried several different approaches , nothing worked. must really, stupid can't find it.

change classes

document.getelementbyid("test").classname = test; 

add classes:

document.getelementbyid("test").classname += test; 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -