Hi everyone, i’m new to JavaScript so i picked up the book Simply JavaScript, to learn the basics of JavaScript, using the book i tried to construct a simple event listener which would, when someone clicked the text, change the style of the text so that it would be white, however when i run the script, the text turns white immediately without any clicks from the user…:(.
here is my code, by the way if your not familiar with the book, it has source code with it that gives you a core.start() function that starts the code after the page loaded.
var HideText =
{
init: function()
{
var text = document.getElementsByTagName('p');
for (var i =0; i <text.length; i++)
{
text[i].addEventListener('click',HideText.addClass(text[i]), false);
}
},
addClass: function(text)
{
if (text.className == "")
{
text.className = 'hiddentext';
}
else
{
text.className += " " + 'hiddentext';
}
}
};
Core.start(HideText);
and here is the html code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello World</title>
<link type="text/css" rel="stylesheet" href="helloworld.css" />
<script language="javascript" type="text/javascript" src="../../core.js"></script>
<script language="javascript" type="text/javascript" src="helloworld.js"></script>
</head>
<body>
<p>Hello World!</p>
</body>
</html>