SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Div onClick not triggering function in Safari

    I have HTML code that reads:

    Code html:
    <div onClick="javascript:update_status();" class="profile_status" style="cursor: pointer">
    <!-- my interesting profile status update here -->
    </div>
    What's interesting is that the javascript function update_status() fires in Firefox when I click the div, but in Safari it does not. Any suggestions or ideas?

  2. #2
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't need the javascript: in the onclick attribute. An onclick assumes that what follows is a script. I have re-written the snippet below, which now works in all browsers.
    <html>

    <head>

    <script type="text/JavaScript">
    <!--
    function update_status() { alert("update"); }
    //
    //-->
    </script>
    </head>

    <body>

    <div class="profile_status" style="cursor: pointer" onclick="update_status()">
    <p>aaaaaaa</div>
    <!-- end div -->

    </body>

    </html>

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent. I have changed my code accordingly.

    I made a slight mistake though... the onclick event isn't firing in CHROME, not Safari. My bad. Any ideas as to why this wouldn't fire in Chrome?

  4. #4
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    317
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Getting same issue; onClick doesn't seem to work at all :/

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this a Chrome bug? I can't seem to find anything anywhere else on the Internet about this.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ole90 View Post
    Getting same issue; onClick doesn't seem to work at all :/
    Have you tried typing it correctly as onclick without the capital C. Perhaps Chrome is case sensitive for that command and doesn't allow you to mistype it with capitals the way other browsers allow in HTML (they all insist on the correct lowercase when you use it in JavaScript).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I left the code alone for a couple of weeks and now the onclick magically works in Chrome without modification. It's possible that this was a bug.


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
  •