How to create Mini Horizontal Radio button controlgroups using jQuery Mobile?


Radio buttons are a useful user interface element that allow users to select a single option from a group of choices. jQuery Mobile provides a framework for creating mobile-optimized websites, and offers a simple way to create radio button controlgroups. In this article, we will guide you through the process of creating mini horizontal radio button controlgroups using jQuery Mobile. This tutorial is ideal for web developers who want to create user-friendly interfaces for mobile devices, and for those who are new to jQuery Mobile and want to learn more about its features.

Getting Started with jQuery Mobile

Before we dive into the process of creating a flip toggle switch, you need to make sure you have the jQuery Mobile library included in your project. You can download it from the official website or include it from a CDN.

fieldset Tag

The fieldset tag is an HTML tag used to group related elements in a form. The fieldset tag is often used in conjunction with the legend tag to provide a label for the group of form elements.

Syntax

<fieldset>
   <!-- form elements go here -->
</fieldset>

data-role Attribute

The data-role attribute is a special attribute used in jQuery Mobile to enhance the functionality and appearance of HTML elements. The data-role attribute specifies the role of the element in the mobile user interface, such as a header, footer, content area, or form element.

Syntax

<element data-role="role">

Here are some of the most common values for the data-role attribute in jQuery Mobile −

  • "header" − Specifies a header for the page.

  • "footer" − Specifies a footer for the page.

  • "content" − Specifies the main content area for the page.

  • "page" − Specifies a page in a multi-page template.

  • "listview" − Specifies a list view for displaying items.

  • "form" − Specifies a form for collecting data.

  • "button" − Specifies a button.

  • "checkbox" − Specifies a checkbox.

  • "radio" − Specifies a radio button.

Approach

The course of action can be disintegrated into the subsequent measures −

  • Create the form elements − First, you will need to create the radio buttons that you want to include in the controlgroup. This can be done using the input tag with a type attribute of "radio".

  • Wrap the form elements in a container − Next, you will need to wrap the radio buttons in a container element, such as a div or a fieldset. This container element will be used to apply the styles and behavior of the controlgroup.

  • Assign the data-role attribute − You will need to assign the data-role attribute to the container element and set its value to "controlgroup". This will tell jQuery Mobile to treat the container as a controlgroup and apply the appropriate styles and behavior.

  • Add the data-type attribute − To specify the orientation of the controlgroup, you will need to add a data-type attribute to the container element and set its value to "horizontal". This will ensure that the radio buttons are displayed horizontally in the controlgroup.

  • Add the data-mini attribute − To make the controlgroup mini-sized, you will need to add a data-mini attribute to the container element and set its value to "true". This will make the controlgroup smaller and more compact.

  • Include jQuery Mobile − Ultimately, it will be necessary for you to incorporate the jQuery Mobile library into your undertaking to leverage its functionalities and styles. This can be achieved by incorporating the necessary hyperlink to the library into your HTML file.

Example

The following is the final code which we are going to look at in this example −

<!DOCTYPE html>
<html>
<head>
   <title>How to create Mini Horizontal Radio button controlgroups 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>
</head>
<body>
   <h4>How to create Mini Horizontal Radio button controlgroups using jQuery Mobile?</h4>
   <div data-role="fieldcontain">
      <fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
         <legend>Choose a pet:</legend>
         <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2a" value="on" checked="checked">
         <label for="radio-choice-h-2a">Cat</label>
         <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2b" value="off">
         <label for="radio-choice-h-2b">Dog</label>
         <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2c" value="other">
         <label for="radio-choice-h-2c">Fish</label>
      </fieldset>
   </div>
</body>
</html>

Conclusion

In summary, crafting diminutive horizontal controlgroups of radio buttons utilizing jQuery Mobile is an uncomplicated procedure that necessitates the proper utilization of data attributes and form elements. By adhering to the guidelines presented in this article, you can develop an aesthetically pleasing and intuitive interface for your mobile website. This instructional material establishes a strong groundwork for utilizing jQuery Mobile, and with further acquaintance with the framework, you can construct even more intricate interfaces. With its simplicity and thorough documentation, jQuery Mobile is an excellent option for web developers seeking to design mobile-oriented websites.

Updated on: 10-Apr-2023

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements