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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    184
    Thanks
    68
    Thanked 2 Times in 2 Posts

    Why is this code not working? (basic example)

    Code:
    <html>
    <head>
    <title>Click the button</title>
    <script type="text/javascript">
    
    function x() {
    	window.alert('1');
    	window.alert('2');
    	window.alert('3');
    	}
    
    document.getElementById("y").onclick = x;
    
    </script>
    </head>
    <body>
    <form>
    <input type="button" value="Click the button" id="y" />
    </form>
    </body>
    </html>
    By the way... Are there any good websites / validators that can be helpful in situations like that? I've been looking at this one so far:

    http://www.jslint.com/

    Thanks.
    Last edited by joliett89; 10-05-2011 at 06:13 PM.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Code:
    window.onload = function() {
    document.getElementById("y").onclick = x;
    }
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    joliett89 (10-05-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    184
    Thanks
    68
    Thanked 2 Times in 2 Posts
    I need to do the following things though:

    1) Write a function named x that will send three alerts to the viewer
    2) Create a button in HTML document with an id name of y
    3) Write a code in JS so the x function will execute when a user clicks on the button

    Something like that doesnt work either:

    Code:
    var varName=document.getElementById("y");
    varName.onclick=x;
    Thats in place of:

    Code:
    document.getElementById("y").onclick = x;
    Last edited by joliett89; 10-05-2011 at 06:27 PM.

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    well, if you're not taking the first suggestion which obviously was working, then try:
    Code:
    <html>
    <head>
    <title>Click the button</title>
    </head>
    <body>
    <form>
    <input type="button" value="Click the button" id="y" />
    </form>
    <script type="text/javascript">
    
    function x() {
    	window.alert('1');
    	window.alert('2');
    	window.alert('3');
    	}
    
    //document.getElementById("y").onclick = x;
    var varName=document.getElementById("y");
    varName.onclick=x;
    </script>
    </body>
    </html>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    joliett89 (10-05-2011)

  • #5
    Regular Coder
    Join Date
    Jul 2011
    Posts
    184
    Thanks
    68
    Thanked 2 Times in 2 Posts
    Thanks, I did not realize that placing the code under the input form makes a difference here.

  • #6
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Placing the script under window.onload function is vital so that the script will trigger once the page has fully reload.

    ...but since you're not taking that suggestion, placing the script after the element has loaded can do the job too.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    joliett89 (10-05-2011)


  •  

    Posting Permissions

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