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 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question about DIV and JavaScript

    I have this line of code:

    <div class="blue" onclick="myfunction()" id="blue2">

    This works fine and makes the JavaScript work if you click the Div.

    Is there a way to make it so you don't have to click it, but it works automatically upon page load?

    Thanks.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    Code:
    <div class="blue" id="blue2">
    <script>
    myfunction();
    </script>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    <body onload = "myfunction()">
    <div class="blue" id="blue2">
    Content of Div
    </div>

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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