Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2007
    Location
    Lisboa
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Angry simple alert not working!

    could someone please tell me why this simple code is not working? i tried all variation i could remember, i click and click, tried in firefox IE, netscape, and nothing:

    <html>
    <head>
    <title>behavior</title>
    <script language="javascript" type="text/javascript">
    var parag = document.getElementsByTagName("p");
    for(var i=0; i<parag.length; i++) {
    parag[i].onclick = function() {
    alert("click!");
    }
    }
    </script>
    </head>
    <body>
    <p>one paragraph</p>
    <p>other paragraph</p>
    </body>
    </html>

    any kind of help wuld ge appreciated.

  • #2
    Regular Coder BonRouge's Avatar
    Join Date
    Mar 2006
    Location
    Sendai, Japan
    Posts
    328
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
    * {
    margin:0;
    padding:0;
    }
    body {
    background-color:white;
    }
    </style>
    <script type="text/javascript">
    function click() {
    var parag = document.getElementsByTagName("p");
    	for( i=0; i<parag.length; i++) {
    		parag[i].onclick = function() {
    		alert("click!");
    		}
    	}
    }
    window.onload=click;
    </script>
    </head>
    <body>
    <p>one paragraph</p>
    <p>other paragraph</p>
    
    </body>
    </html>

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    ...and the reason is:

    The loading process works sequentially (well, not exactly, but to simplify it for your problem, let's say it does so). So that the moment you try to find the element (with javascript placed in head), that element is not yet loaded (in the body)

    Most of the time javascript uses functions called by an event, following an user's action. The user's action is, in your case loading the page. For this action javascript uses window.onload event. By short onload
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Feb 2007
    Location
    Lisboa
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    you are right. i think that is why calling the function with

    <script>
    function click(){}
    window.onload=click();
    </script>

    doesn't work and

    <body onLoad="click();">

    does work. thanks


  •  

    Posting Permissions

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