How Do I Change to OnClick?

Discussion in 'Javascript' started by Jenny, Sep 19, 2005.

  1. Jenny

    Jenny Guest

    Hi - There's a neat little snowfall effect at
    http://javascript.internet.com/bgeffects/snow.html. I want to use it for a
    school project, but I need to run it by clicking on a button, rather than
    when the page loads. In the HTML, I want to use something like:

    <FORM>
    <INPUT TYPE="button" value="Winter Time" onClick="?????">
    </FORM>

    but I cannot figure out what to do with the actual script to make this work.
    I'd appreciate any suggestions. BTW, I'm a serious novice, so please keep it
    as simple as possible. Thank you.
    Jenny
     
    Jenny, Sep 19, 2005
    #1
    1. Advertisements

  2. Jenny

    ASM Guest

    you delete :

    if (ns4up) {
    snowNS();
    } else if (ie4up) {
    snowIE();
    }

    from the script

    and enter it in your onclick

    onclick="if(ns4up) snowNS(); else if(ie4up) snowIE();"
     
    ASM, Sep 19, 2005
    #2
    1. Advertisements

  3. Jenny

    McKirahan Guest

    Place this (exisiting) code in a function:

    if (ns4up) {
    snowNS();
    } else if (ie4up) {
    snowIE();
    }

    Then have a link invoke the function.

    Such as:

    function Snowflakes() {
    if (ns4up) {
    snowNS();
    } else if (ie4up) {
    snowIE();
    }
    }

    <a href="javascript:Snowflakes()">Snowflakes</a>

    or

    <img src="http://javascript.internet.com/img/snow/snow.gif"
    border="0" width="24" height="24" onclick="Snowflakes()"
    alt="Snowflakes" title="Snowflakes">
     
    McKirahan, Sep 19, 2005
    #3
  4. Jenny

    web.dev Guest

    Hi Jenny,

    I'm going to assume you followed the exact instructions as that site
    told you.
    Secondly, towards the end of the code you should see the following
    lines:

    if (ns4up) {
    snowNS();
    } else if (ie4up) {
    snowIE();
    }

    Replace those lines with the following:

    function startSnow()
    {
    if(ns4up)
    snowNS();
    else if(ie4up)
    snowIE();
    }

    Finally, in your HTML you can do what you wanted:

    <input type = "button" value = "Winter Time" onClick = "startSnow()"/>

    Hope this helps :)
     
    web.dev, Sep 20, 2005
    #4
  5. Jenny

    Jenny Guest

    Thanks to everyone for your help. It worked great! Thanks again, Jenny
     
    Jenny, Sep 20, 2005
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.