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 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    40
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Button switch text and call different functions on onclick

    Hi,
    This button changes the button text when clicked, but it is always calling the same function. How to make the button depending on the button text to call different functions on onclick?

    [CODE]
    <script type = "text/javascript">
    function button_switch(){
    if (document.switch_form.switch_b.value == "Adam"){
    document.switch_form.switch_b.value="Eva";
    }
    else{
    document.switch_form.switch_b.value = "Adam";
    }
    }
    </script>

    <form name = "switch_form">
    <input type = "button" name = "switch_b" id = "switch_b" value = "Adam" onclick = "button_switch()" />
    </form>
    [CODE]

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    just add the function calls to your if/else...

    Code:
    <script type = "text/javascript">
    function button_switch(){
    if (document.switch_form.switch_b.value == "Adam"){
    document.switch_form.switch_b.value="Eva";
    one();
    }
    else{
    document.switch_form.switch_b.value = "Adam";
    two();
    }
    }
    
    function one() {
    alert("you clicked Adam") 
    }
    
    function two() {
    alert("you clicked Eva") 
    }
    </script>

  • Users who have thanked xelawho for this post:

    JonesJ (12-22-2011)


  •  

    Tags for this Thread

    Posting Permissions

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