Search code examples
javascriptjqueryandroidjquery-mobile

How to change the jquery mobile flip switch state from code


I have some jQuery Mobile flip toggle switches on my Android/iPad application, and I need to change their states (on/off) dynamically, using JavaScript. I was looking for a solution here, (Change value of flip toggle dynamically with jQuery Mobile) and I tried several ways (.val('on'), .slider('enable')...) but it seems the control is not working at all.

Is there a solution for this issue? How can I do to change the flip switch state from code?


Solution

  • I've examined the page you posted and I confirmed that the solution:

    $('selector').val('value').slider('refresh');
    

    does indeed work. Make sure that 'selector' is referencing your select element, and that 'value' is a value you defined on the option element you wish to enable.

    I confirmed this by visiting http://jquerymobile.com/demos/1.0.1/docs/forms/switch/index.html, then with firebug's console entering the line:

    $("#flip-b").val('no').slider('refresh');
    

    It switched the second slider displayed on the page from yes to no.