Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

one button one function on first click another on second

Tags:

jquery

I would like one button to perform two jquery functions. One on the first click then the other on the second click. Here is the code for the two buttons. Is there a way to combine the functions?

<input type="button" value="View Page" onclick="ViewPage()" />

<script type="text/javascript">
function ViewPage() {
example_animate('-=50%');
$("#mainContent").toggle();
}
</script>

<input type="button" value="View Menu" onclick="ViewMenu()" />

<script type="text/javascript">
function ViewMenu() {
example_animate('+=50%');
$("#mainContent").toggle();
}
</script>
like image 602
Altered Avatar asked Nov 14 '12 21:11

Altered


People also ask

How do you execute a different function each time the same button is clicked in Javascript?

On click of the button have a function which decides which function to call according to number of times. So first time it will execute function one and second time onwards it will execute function two . Show activity on this post. The simplest way is defining an extra variable to false (or true , of course).

How do you click two buttons at the same time in HTML?

The two methods are to 1) create a new form, put the correct results in that (i.e. use a hidden field with the correct "submit value"), and then submit() it or 2) create an AJAX request (i.e. $. AJAX) and supply the desired data.

How do I show a form on the same page when I click on a button?

You can add an onclick attribute to the button that changes the display to block.


3 Answers

Try something like this - DEMO

var action = 1;

$("input").on("click", viewSomething);

function viewSomething() {
    if ( action == 1 ) {
        $("body").css("background", "honeydew");
        action = 2;
    } else {
        $("body").css("background", "beige");
        action = 1;
    }
}

So in your case it will be

function viewSomething() {
    if ( action == 1 ) {
        example_animate('-=50%');
        action = 2;
    } else {
        example_animate('+=50%');
        action = 1;
    }
    $("#mainContent").toggle();
}
like image 122
Zoltan Toth Avatar answered Dec 14 '22 05:12

Zoltan Toth


Another way using one method, both will fire only once

$("input").one("click", viewSomething);
function viewSomething()
{
    $("body").css("background", "honeydew");
    $("input").one("click", function(){ // on can be used
        $("body").css("background", "beige");
    });
}

DEMO.

Update: If you want to keep the second handler alive then replace one with on like this one.

like image 35
The Alpha Avatar answered Dec 14 '22 03:12

The Alpha


You can also assign a name to html element..

<input type="button" value="Click me" onclick = 'viewSomething(this)'/>           
<script>
    function viewSomething(e){  

        //First time click
        if(e.name != 'Click'){
           e.name = "Click";
           <!--do whatever..-->
         }//end if

       //When click it again..
       else if(e.name == 'Click'){
           e.name = "Unclick";
           <!--do whatever-->
       }//end else
  }//end function
</script>
like image 27
Tapasit Suesasiton Avatar answered Dec 14 '22 05:12

Tapasit Suesasiton