Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I press the button it works, but the second event "onmouseup" is activated when I release the button and the ajax feature becomes mistake. Does the script have anything incorrect?

What I have tried:

HTML
<button id="myP" onmousedown="mouseDown_b()" onmouseup="mouseUp_b()">Click Me</button>


JavaScript
      <script>
          function mouseDown_b() {
        //e.preventDefault();
        document.getElementById("myP").style.color = "red";

        $.ajax({
            type: "POST",
            url: "Default.aspx/testMethod_a",
            data: "{ currentState: 1 }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            //async:false,
            success: function () {
                console.log("success1");
            },
            error: function () {
                alert("error");
            }
        });

    }

    function mouseUp_b() {
        //e.preventDefault();
        document.getElementById("myP").style.color = "green";

        $.ajax({
            type: "POST",
            url: "Default.aspx/testMethod_a",
            data: "{ currentState: 2 }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            //async:false,
            success: function () {
                console.log("success2");
            },
            error: function () {
                alert("2nd_error");
            }
        });
    }
</script>
Posted
Updated 12-Aug-19 2:01am
Comments
[no name] 10-Aug-19 15:12pm    
You should stick to "clicks". This isn't a "desktop" app.
Faisal_Raj 10-Aug-19 22:19pm    
Can you please give me example that how can i do that ?

1 solution

Well, your code works fine. To fire up second time mouse events, move mouse cursor out of button and over it again. Then click the button. I tested it here: JSFiddle[^]


I have no idea why this happen and haven't found any documentation related to this problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900