Search code examples
javascriptjqueryhtmljquery-mobile

How do I make a popup screen to appear once I toggle a flipswitch?


I'm using jQuery Mobile to create a school project.

I'm looking to have a toggle button with Yes/No assigned, and when the option is toggled, I would like a corresponding popup to appear to confirm the change.

What would the best way to perform this, because I have tried a couple of variations with no luck. Thanks!

<!--jQuery CDN Hosted Files-->
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <form id="flipswitchTeacher">
            <label for="flipswitch">Are you available to teach?</label>
            <select name="flipswitch" id="flipswitch" data-role="flipswitch" data-corners="false">
                <option a href="#confirmTeacherNo" value="no">NO</option>
                <option a href="#confirmTeacherYes" value="yes">YES</option>
            </select>
        </form>


        <div data-role="popup" id="confirmTeacherNo" class="ui-content" data-theme="a">
        <p>Confirm No</p>
        </div>

        <div data-role="popup" id="confirmTeacherYes" class="ui-content" data-theme="a">
        <p>Confirm Yes</p>
        </div>

Solution

  • If you review the details found here: https://api.jquerymobile.com/popup/ you will see:

    Using the markup-based configuration, when a link with the data-rel="popup" is tapped, the corresponding popup container with the id referenced in the href of the link will be shown. To open a popup programmatically, call popup with the open method on the popup container:

    $( "#myPopupDiv" ).popup( "open" )

    This can be executed like so:

    $(function() {
      $("#flipswitch").change(function(e) {
        var target = $("option:selected", this).attr("href");
        $(target).popup("open");
      });
    });
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    
    <form id="flipswitchTeacher">
      <label for="flipswitch">Are you available to teach?</label>
      <select name="flipswitch" id="flipswitch" data-role="flipswitch" data-corners="false">
        <option href="#confirmTeacherNo" value="no">NO</option>
        <option href="#confirmTeacherYes" value="yes">YES</option>
      </select>
    </form>
    
    
    <div data-role="popup" id="confirmTeacherNo" class="ui-content" data-theme="a">
      <p>Confirm No</p>
    </div>
    
    <div data-role="popup" id="confirmTeacherYes" class="ui-content" data-theme="a">
      <p>Confirm Yes</p>
    </div>

    Hope that helps.