SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Threaded View

  1. #1
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question event handler executing before event

    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.

    Code:
    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:
    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>
    Last edited by maxdream01; May 13, 2012 at 10:42. Reason: typo
    PHP is FUN


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •