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 5 of 5
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onclick for appended image not working

    page: www.therabbitshome.com

    Click "BROWSE GALLERIES", then "(2010-2011) GALLERY 1". Now click the first thumbnail picture to the left. An alert box should come up, but it doesn't.

    Code:
    var gOutOf=document.createElement('img');
    Code:
    gOutOf.onclick="clickGOutOf()";
    Code:
    function clickGOutOf() {
        alert("clicked");
    }
    Help pls?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,931
    Thanks
    56
    Thanked 552 Times in 549 Posts
    either

    Code:
    gOutOf.onclick=clickGOutOf;
    or
    Code:
    gOutOf.onclick=function(){clickGOutOf()};

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    o

    thx.

  • #4
    New Coder
    Join Date
    May 2012
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And if I may say so, it makes no sense to me why the format would be different in the markup and in JS. But that's javascript I guess...nonsensical.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,931
    Thanks
    56
    Thanked 552 Times in 549 Posts
    the simple answer would be that html is html and javascript is javascript.

    the slightly longer answer would be that in html, an inline event handler like onclick accepts javascript between the quotes. But wrapping something in quotes in javascript converts it into a string, meaning that the function will never execute.

    that's my take on it, anyway - makes sense if you think about it.


  •  

    Posting Permissions

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