Changing Jquery Mobile FlipSwitch selected value Programmatically

4.3k views Asked by At

I am trying to change flip switch value programmatically but its not working here is the HTML

<label for="flip-1">Flip switch:</label>
<select name="flip-1" id="flip-1" data-role="slider">
    <option value="NG">NG</option>
    <option value="OK">OK</option>
</select>
<input type="text" id="name" />
<input type="number" id="age" />

Here is the Script

$("#flip-1").on('slidestart', function (event) {
    var name = document.getElementById("name").value;
    var age = document.getElementById("age").value;
    if (name == "" || age == "") {

        $("#flip-1").val("NG").flipswitch("refresh");
    } else[
    // slide it to OK
    ]
});

What i need is that NG to be selected all the time but only if there is some text in both inputs

Here is the Fiddle http://jsfiddle.net/r9X5U/6/

2

There are 2 answers

0
Aravin On BEST ANSWER

Try like the following it will work. Set change event for flip switch.

HTML is like

<select  name="flip-3" id="snd-switch" data-role="flipswitch" data-mini="true">
    <option value="NG">NG</option>
    <option value="OK">OK</option>
</select>
<input type="text" id="name" />
<input type="text" id="age" />

Javascript is like:

$("#snd-switch").on('change', function (event) {
var name = document.getElementById("name").value;
var age = document.getElementById("age").value;
if(name == "" || age == ""){
   // alert("hi");
    $("#snd-switch").val("NG").flipswitch("refresh");
}else{
   $("#snd-switch").val("OK").flipswitch("refresh");
}
});

Here is the FIDDLE DEMO

0
Sga On

I would reverse the logic, checking the input fields and enabling the flipswitch when needed. HTML:

<div data-role="page" id="page">
    <label for="flip-1">Flip switch:</label>
    <select name="flip-1" id="flip-1" data-role="flipswitch">
        <option value="NG">NG</option>
        <option value="OK">OK</option>
    </select>
    <input type="text" id="name" />
    <input type="number" id="age" />
</div>

JavaScript:

$("#page").bind("pageshow", function()
{
    $("#flip-1").flipswitch("disable");

    $("#name,#age").on("keyup", function(event, ui)
    {
        if ($("#name").val().length !== 0 && $("#age").val().length !== 0)
            $("#flip-1").flipswitch("enable");
        else
            $("#flip-1").flipswitch("disable");
    });    
});