I have a HTML like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div id='first'>
<div id='second'>
<div id='third'>
</div>
</div>
</div>
</body>
</html>
I also have some Javascript code like this:
var element = document.createElement('div');
element.id = 'forth';
var parent = document.getElementById('third');
parent.appendChild(element);
When I run the javascript code, the HTML should change to:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div id='first'>
<div id='second'>
<div id='third'>
<div id='forth'></div>
</div>
</div>
</div>
</body>
</html>
Then after I typed this line below in the Browser's console:
element.className = 'test';
What I hope is that only the variable 'element' in Javascript will change and have the class name 'test'. However, when I check my current HTML it also have the class name 'test':
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div id='first'>
<div id='second'>
<div id='third'>
<div id='forth' class='test'></div>
</div>
</div>
</div>
</body>
</html>
Can anybody explain that? Thanks so much!!
elementvariable is a Javascript reference to that<div>. When you set its class name totest, the<div>pointed to by that variable gets a class name oftest. What were you expecting?