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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Two functions, one "onClick"

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function calcbase(calc,attack){
    if(document.calc.attack.value=='tackle'){
    document.calc.dam.value=8.75
    }
    }
    function calcstab(calc,stab){
    if(document.calc.stab.value=='yes'){
    document.calc.dam.value*=1.5
    }
    }
    </script>
    </head>
    <body>
    <form name="calc">
    <table border="1">
    <tr>
    <td>Attack</td><td><input type="text" name="attack" />
    </tr>
    <tr>
    <td>STAB?</td>
    <td>
    <select name="stab">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td>SE?</td>
    <td>
    <select name="se">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td>UE?</td>
    <td>
    <select name="ue">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td>NVE?</td>
    <td>
    <select name="nve">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td>Critical?</td>
    <td>
    <select name="crit">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td><button type="button" onClick="calcbase(calc,attack);calcstab(calc,stab)">Calculate</button></td>
    <td><input type="text" value="damage" name="dam" readonly="readonly" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    I heard you were able to use two functions in one onClick by separating them with a semicolon. Now I've done that, yet it only carries out one function, the calcbase(calc,attack) one. Anybody mind lending me some help?

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,501
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function calcbase(calc,attack){
    if(document.calc.attack.value=='tackle'){
    document.calc.dam.value=8.75;
    }
    }
    function calcstab(calc,stab){
    if(document.calc.stab.value=='yes'){
    document.calc.dam.value*=1.5;
    }
    }
    </script>
    </head>
    <body>
    <form name="calc">
    <table border="1">
    <tr>
    <td>Attack</td><td><input type="text" name="attack" value="tackle"/>
    </tr>
    <tr>
    <td>STAB?</td>
    <td>
    <select name="stab">
    <option value="0">--
    <option selected="selected" value="yes">Yes
    <option value="no">No
    </select>
    </td>
    </tr>
    <tr>
    <td>SE?</td>
    <td>
    <select name="se">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td>UE?</td>
    <td>
    <select name="ue">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td>NVE?</td>
    <td>
    <select name="nve">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td>Critical?</td>
    <td>
    <select name="crit">
    <option value="0">--
    <option value="1">Yes
    <option value="2">No
    </select>
    </td>
    </tr>
    <tr>
    <td><button type="button" onClick="calcbase(calc,attack);calcstab(calc,stab)">Calculate</button></td>
    <td><input type="text" value="damage" name="dam" readonly="readonly" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was missing a semicolon, huh? Seems simple enough. Thanks, a lot.

  • #4
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, one more question: Can you have more than two functions on one onClick?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Twitch2k7 View Post
    Actually, one more question: Can you have more than two functions on one onClick?
    Indeed you can. onclick = "function1();function2();function3();function4()"

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,501
    Thanks
    3
    Thanked 500 Times in 487 Posts
    the dual function call was fine but not the change to the select values to align with the conditional in the function

    Code:
    <select name="stab">
    <option value="0">--
    <option selected="selected" value="yes">Yes
    <option value="no">No
    </select>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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