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
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts

    onClick event fires when the page loads...

    Alright, so I'm working on a script, and I'm simply using the following code:

    document.getElementById("element").onclick = alert("hello");

    When I load the page, the alert fires instantaneously. I'm completely stumped. I've checked everything in my not too extensive Javascript file, and it looks fine. What common errors could cause this?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,735
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    That is not valid Javascript syntax. Consult your error console.

    What you want is

    Code:
    <span id = "element" onclick = "alert('Hello')">Philip M</span>

    Quizmaster: The cape on which the Pilgrim Fathers landed in 1620 is named after which fish?
    Contestant: Haddock

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,135
    Thanks
    12
    Thanked 332 Times in 328 Posts
    all the event handlers expect a function to be given, not the result of a function call*. i.e.
    PHP Code:
    document.getElementById("element").onclick = function (evt) { alert("hello"); } 
    * - unless that result is a function itself.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Dormilich View Post
    all the event handlers expect a function to be given, not the result of a function call*. i.e.
    PHP Code:
    document.getElementById("element").onclick = function (evt) { alert("hello"); } 
    * - unless that result is a function itself.
    Thank you, I'll give that a try. And Philip, I was hoping to avoid that.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,735
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by iLochie View Post
    And Philip, I was hoping to avoid that.
    Why? Why go for complication? I agree that dormilich's script is "unobtrusive" but why does it matter?

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,135
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by Philip M View Post
    I agree that dormilich's script is "unobtrusive" but why does it matter?
    maintenance, readability & code re-use
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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