How to create Mini Fieldcontain flip toggle switch using jQuery Mobile?


The development of intuitive and user-friendly interfaces is an integral facet of contemporary web design. As web technologies persist in evolving, programmers must keep pace with the latest patterns and resources to offer users the optimal experience. One popular solution for creating interactive mobile-friendly interfaces is jQuery Mobile, a user interface framework built on top of the jQuery library. In this article, we will explore the process of creating a Mini Fieldcontain flip toggle switch using jQuery Mobile. By the end of this tutorial, you will have a solid understanding of how to use jQuery Mobile to create this unique and engaging UI element for your mobile web applications.

Data-role Attribute

The data-role attribute in jQuery Mobile is used to define the role or purpose of an HTML element in the context of a jQuery Mobile-enhanced web page. It is a custom HTML5 data attribute that is used by jQuery Mobile to apply the appropriate styles and behaviors to the element.

Syntax

data-role="role-name"

In this syntax, role-name is a string that specifies the role or purpose of the element in the context of a jQuery Mobile-enhanced web page. There are many different values that can be used for role-name, depending on the type of element and the desired behavior. Here are some common values for the data-role attribute −

  • "page" − Used on the top-level container of a jQuery Mobile page

  • "header" − Used to define a header for a jQuery Mobile page

  • "footer" − Used to define a footer for a jQuery Mobile page

  • "content" − Used to define the main content area of a jQuery Mobile page

  • "button" − Used to define a button element that should be styled and enhanced as a jQuery Mobile button

  • "listview" − Used to define a list element that should be styled and enhanced as a jQuery Mobile list

.Change() Method

The .change() technique in jQuery Mobile is employed to associate an event handler with the "change" event of a form input element, such as a radio button, checkbox, or select list. When the user interacts with the input element, such as by selecting or deselecting a checkbox, the change event is initiated, and any associated event handler functions are invoked.

Syntax

$(selector).change(function(event) {
   // Event handler code here
});

In this syntax, selector is a string representing the jQuery selector that matches the form element to which the event handler should be bound.

Approach

To create a Mini Fieldcontain flip toggle switch using jQuery Mobile, we first need to include the jQuery and jQuery Mobile libraries in our HTML file using the appropriate CDN links. Once we have done this, we can create a new div element with the data-role="fieldcontain" attribute and data-mini="true" attribute to create a compact version of the switch. Inside this div element, we add a label element with the for attribute set to the ID of the input element that will contain the flip toggle switch. Subsequently, we append an input component that has the attribute type set to "checkbox", the attribute data-role assigned to "flipswitch", and both the name and identifier attributes assigned a unique value. Moreover, one has the ability to assign specific verbiage that manifests when the flipswitch is in either the "on" or "off" position by adjusting the data-on-text and data-off-text attributes, in that order. Ultimately, we have the ability to employ customized CSS styles to embellish the flipswitch and its label to suit our preferences, as well as implement JavaScript code to confer added functionality to the flipswitch, such as triggering an alert when it's flipped on or off.

Example

The following code showcases how to create a mini fieldcontain flip toggle switch using jQuery Mobile, which involves including libraries and CSS stylesheets for styling and manipulation. The switch is created within a fieldcontain div that includes a label and checkbox input with data-role and data-on-text attributes. A jQuery script is used to detect changes in the input and display an alert message indicating the current status of the toggle switch. Overall, this code demonstrates how to create and enhance the interactivity of a flip toggle switch with jQuery Mobile.

<!DOCTYPE html>
<html>
<head>
   <title>How to create Mini Fieldcontain flip toggle switch using jQuery Mobile?</title>
   <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-mobile/1.4.5/jquery.mobile.min.css" />
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mobile/1.4.5/jquery.mobile.min.js"></script>
   <style>
      .ui-flipswitch {
         width: 60px;
         height: 30px;
      }
      .ui-flipswitch.ui-flipswitch-active {
         background-color: #2ecc71;
      }
      .ui-flipswitch-label {
         font-size: 12px;
      }
   </style>
</head>
<body>
   <h4>How to create Mini Fieldcontain flip toggle switch using jQuery Mobile?</h4>
   <div data-role="fieldcontain" data-mini="true">
      <label for="flip-checkbox">Toggle:</label>
      <input type="checkbox" data-role="flipswitch" name="flip-checkbox" id="flip-checkbox" data-on-text="On"
      data-off-text="Off">
   </div>
   <script>
      $(document).ready(function() {
         $('#flip-checkbox').change(function() {
            if ($(this).is(':checked')) {
               alert('Switch is on');
            } else {
               alert('Switch is off');
            }
         });
      });
   </script>
</body>
</html>

Conclusion

In this article, we have demonstrated the creation of a compact fieldcontain flip toggle switch using jQuery Mobile. By adhering to the procedure we've elucidated, you can conveniently include this feature to your mobile-oriented websites or web-based applications, and provide your audience with a swift and instinctive way to switch between two alternatives. We've also emphasized the malleability of this feature and how it can be adjusted to suit a wide range of design necessities. With a sprinkle of CSS styling, you can modify the switch to harmonize the appearance and sensation of your website or app.

Updated on: 13-Apr-2023

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements