Create Horizontal Selects Controlgroups in jQuery Mobile


The Horizontal Select Control Group is an interface component that enables users to choose from a range of choices that are presented horizontally.

Step 1: Include jQuery Mobile in your project

Include jQuery Mobile in your project as the first stage in creating a Horizontal Select Control Group. jQuery Mobile's most recent version can be added to your HTML page directly or through a content delivery network to accomplish this (CDN).

Algorithm

  • Load the jQuery Mobile CSS file from the given CDN URL

  • Load the jQuery library from the given CDN URL

  • Load the jQuery Mobile JS file from the given CDN URL

Example

<!-- Include jQuery Mobile CSS -->
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">

<!-- Include jQuery Mobile JS -->
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

Step 2: Create the Horizontal Select Control Group

Once jQuery Mobile has been added to your project, use the HTML markup to add a horizontal select control group.

Algorithm

  • Identify the container element with the data-role attribute set to "controlgroup" and data-type attribute set to "horizontal".

  • Create child elements inside the container element to represent the individual options.

  • For each option, create an element with the following attributes:

    • href attribute set to the link URL (in this case, "#").

    • class attribute set to "ui-btn ui-corner-all ui-shadow".

    • Text content representing the option label.

    • Repeat step 3 for each option.

    • Place the elements inside the container element as children.

    • Use CSS to style the options and container elements as desired.

Example

<div data-role="controlgroup" data-type="horizontal">
  <a href="#" class="ui-btn ui-corner-all ui-shadow" data-icon="star">Option 1</a>
  <a href="#" class="ui-btn ui-corner-all ui-shadow" data-icon="check">Option 2</a>
  <a href="#" class="ui-btn ui-corner-all ui-shadow">Option 3</a>
  <a href="#" class="ui-btn ui-corner-all ui-shadow">Option 4</a>
  <a href="#" class="ui-btn ui-corner-all ui-shadow">Option 5</a>
</div>

Step 3: Initialize the Horizontal Select Control Group

We must start the Horizontal Select Control Group with JavaScript for it to work.

Algorithm

  • When the document is created or loaded, the function is called.

  • The function selects all the div elements that have a data-role attribute equal to "controlgroup".

  • The "controlgroup()" function is called on the selected elements to initialize the control group.

  • The "container()" method called on the control group to retrieve its container element.

  • The "enhanceWithin()" method is called on the container element to enhance its child elements.

  • The "css()" method is called on the enhanced container element to set its width property to 100%.

Example

$(document).on("pagecreate", function() {
  $("div[data-role='controlgroup']").controlgroup().controlgroup("container").enhanceWithin().css("width", "100%");
});

Conclusion

There are certain constraints that must be considered for optimal user experience.

  • Width of each option must be equal to maintain consistent display.

  • The number of options should be limited to the available screen space to avoid overlap and scrolling.

  • It is recommended to use horizontal select control groups on larger screens rather than on smaller screens.

  • Thorough testing on various devices and screen sizes is crucial to ensure that the control group is responsive and functions correctly.

By keeping these constraints in mind, developers can create horizontal select control groups that enhance the usability and functionality of their mobile web applications.

Updated on: 21-Aug-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements