Solved

jquery mobile flip switch

Posted on 2014-11-07
1
644 Views
Last Modified: 2014-11-14
I am using a jquery mobile flip switch.

I have the "change" event bound to it, so that when the user changes it, it sends an ajax request to the server to change the state of the switch.

$("#myswitch").bind("change", function (event){
    // do ajax call to server
});

The client also polls the server, and If a different users on another browser somewhere changes the flip switch, then the switch on this browser is changed programatically:

$('#myswitch').val('on').slider("refresh");

The problem is that this slider refresh causes the change event to fire, when then sends ajax back to the server, which results in a loop.

Is there a way to know in the change event whether the event was fired because the user actually changed the switch, or if the switch was changed programmatically?
0
Comment
Question by:rustycp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40430030
try this :
$("#myswitch").bind("change", function (event){
    if( !event.isTrigger ) {  // if not programmatically
        // do ajax call to server
    }
});

Open in new window

or this :
$("#myswitch").bind("change", function (event){
    if( event.originalEvent ) {  // if not programmatically
        // do ajax call to server
    }
});

Open in new window

0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question