Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery mobile click event fires twice

I have a problem with a simple JQuery mobile app. It all boils down to the the click event firing twice.

No solution to similar problem has resolved this issue.

The problem happens whether deploying to device or displaying in browser.

Here's the code

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />

<script src="jquery-1.8.2.min.js" type="text/javascript"></script>  
<script src="jquery.mobile-1.2.0.min.js" type="text/javascript"></script>
<script src="cordova-2.2.0.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).bind("mobileinit", function () {
        $.support.cors = true;
        $.mobile.allowCrossDomainPages = true;
    });
</script>

<link rel="Stylesheet" href="jquery.mobile-1.2.0.min.css" />

<div>
    <input type="button" id="ButtonSubmit" value="Save" />
</div>

<script type="text/javascript">


    $("#ButtonSubmit").click(function () {
        alert('Clicked');
    });

</script>

like image 321
Alex Cooper Avatar asked Dec 01 '12 14:12

Alex Cooper


2 Answers

You can solve that by using below mentioned way.

Use firstly unbind and then bind like below :

<script type="text/javascript">

    $("#ButtonSubmit").unbind('click').bind('click', function () {
            alert('Clicked');
            return false; //to prevent the browser actually following the links!
        });

</script>

Update: If you're having on method then you could use is as below.

Note: When you're using off method it's removing previous click event handler and with on method it adds new one.B'cos of that it does not allow to happen click 2 times.

<script type="text/javascript">

$('#ButtonSubmit').off('click').on('click', function(){

  alert('Clicked');
  return false; //to prevent the browser actually following the links!
}); 

</script>
like image 137
Sampath Avatar answered Sep 21 '22 01:09

Sampath


There's lots of experimental stuff going on in the latest jQuery mobile with firing click events, which is probably causing these issues (line #2689):

// This plugin is an experiment for abstracting away the touch and mouse
// events so that developers don't have to worry about which method of input
// the device their document is loaded on supports.
//
// The idea here is to allow the developer to register listeners for the
// basic mouse events, such as mousedown, mousemove, mouseup, and click,
// and the plugin will take care of registering the correct listeners
// behind the scenes to invoke the listener at the fastest possible time
// for that device, while still retaining the order of event firing in
// the traditional mouse environment, should multiple handlers be registered
// on the same element for different events.
//
// The current version exposes the following virtual events to jQuery bind methods:
// "vmouseover vmousedown vmousemove vmouseup vclick vmouseout vmousecancel"

http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.js

The way I fixed it was:

$('#mobileMenuItem').off('click');

and then it began functioning normally.

like image 24
Alex W Avatar answered Sep 22 '22 01:09

Alex W