SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Why is onClick Firing Immediately?

    Hello there.

    This is baffling me. I haven't done any Javascript in quite awhile, but I know this is supposed to work. Somehow it isn't;

    HTML Code:
    window.onload = function()
    	{
    	document.getElementById('Track01_Bubble01').onclick = myFunction();
    	}
    Code:
    function myFunction()
    	{
    	alert('Hello World!');
    	}
    Somehow the function is being triggered as soon as the page loads, and it's not when I click on 'Track01_Bubble01'.

    Does anyone see anything that I'm missing? I'm totally stuck.
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  2. #2
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    hmm... Dropping it into an anonymous function seems to have solved it;

    HTML Code:
    <script type="text/javascript">
    	window.onload = function()
    		{
    		document.getElementById('Track01_Bubble01').onclick = function()
    			{
    			myFunction();
    			}
    		}
    </script>
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,270
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.onload = function()
    	{
    	document.getElementById('Track01_Bubble01').onclick = myFunction();
                                                                            ^^^
    Because of these parentheses, you're executing the function and assigning its return value rather than assigning a reference to the function itself.
    	}
    "First make it work. Then make it better."

  4. #4
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott
    Code:
    window.onload = function()
    	{
    	document.getElementById('Track01_Bubble01').onclick = myFunction();
                                                                            ^^^
    Because of these parentheses, you're executing the function and assigning its return value rather than assigning a reference to the function itself.
    	}
    AHA!

    Thanks very much, Jeff!
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse


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
  •