How to make Basic selects using jQuery Mobile?


More and more people in the modern world are utilizing mobile devices to access the internet. So, it's crucial to make sure that web apps are responsive as well as mobile-friendly in design. In this situation, jQuery Mobile is useful. It is a powerful JavaScript package that makes it easier to build responsive and mobile-friendly web applications.

The ability to create dynamic user interfaces that react to user actions in real time is one of jQuery Mobile's key features. This makes it straightforward to design choices that let users pick from a list of options.

This article will concentrate on utilizing jQuery Mobile to build straightforward selections. We'll go through the fundamentals of utilizing the <select> and <option> elements to construct simple selections and then look at some advanced methods for modifying the look and behavior of simple selections.

Select in jQuery

The numerous attributes and content for the user input information are controlled by the jQuery Choose Option. It is a unique feature that is primarily used in the dropdown list. The user can use it as a reference for the data or material they have entered. According to user needs, it regulates and adjusts the multiple-choice information. The form tag of websites or web apps is where it is most frequently utilized.

Approaches

To make a basic selection utilizing jQuery Mobile we can follow the two methods −

  • Utilizing the selectmenu() function in JQuery UI.

  • Utilizing only using the select and option tag in JQuery Mobile.

Let us look into both approaches −

Approach 1: Utilizing the selectmenu() function in JQuery UI

The selectmenu() duplicates and expands the capabilities of a native HTML to choose an element to get over the original control's restrictions.

A replaceable select element is offered by the selectmenu widgets. When a form is submitted or serialized, it will operate as a proxy back to the original select element, controlling its state.

Algorithm

To make a basic selection utilizing the selectmenu() with jQuery, follow these steps −

  • Step 1 − Include the jQuery scripts for jquery mobile and CSS styling.

  • Step 2 − Utilize the label to provide names to the options given.

  • Step 3 − Utilize <select> tag to provide a set of options.

  • Step 4 − In <option> tag, provide all options required to be in the options set. These tags must be inside the <select> tag.

  • Step 5 − Use id=“select-options” and in javascript, with the help of selectmenu() the functionality of the select menu is set.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Basic Select in JQuery Mobile</title>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
   <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
   <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
</head>
<body>
   <div data-role="page">
      <div data-role="header">
         <h1>Basic Select in JQuery Mobile</h1>
      </div>
      <div data-role="main" class="ui-content">
         <label for="select-options" class="select">Select any option:</label>
         <select name="select-choice-1" id="select-choice-1">
            <option value="option1">First option</option>
            <option value="option2">Second option</option></option>
            <option value="option3">Third option</option>
         </select>
      </div>
   </div>
   <script>
      $(document).ready(function(){
         $('#select-options').selectmenu();
      });
   </script>   
</body>
</html>

Approach 2: Utilizing only using the select and option tag in JQuery Mobile

The select and options menu can itself create the basic select with jQuery mobile.

Algorithm

To make a basic selection utilizing the simple select and option tag with jQuery, follow these steps −

  • Step 1 − Include all jQuery scripts to execute code with jQuery.

  • Step 2 − Label the options with label tags for select options.

  • Step 3 − Create a select tag with a name attribute and an id attribute.

  • Step 4 − Use option tags for adding multiple options in Select.

Example

<!DOCTYPE html>
<html>
<head>
   <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>
   <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
</head>
<body>
   <center>
      <h4>Basic Select in JQuery Mobile</h4>
   </center>
   <form>
      <div data-role="page">
         <label for="SelectOption">
            Select any option:
         </label>
         <select name="SelectOption" id="SelectOption">
            <option value="option1">First option</option>
            <option value="option2" selected>Second option</option>
            <option value="option3">Third option</option>
         </select>
      </div>
   </form>
</body>
</html>

Conclusion

Simple choices can be created in a variety of methods for online applications using jQuery Mobile. In this article, two methods were covered: using jQuery UI's selectmenu() function and jQuery Mobile's select and option tags alone. Both strategies are simple to understand and simple to apply. While utilizing simply choosing an option tag is easier and suitable for basic selections, the selectmenu() function offers more sophisticated functionality and may be beneficial in complex applications. Overall, jQuery Mobile is an effective tool for developing dynamic user interfaces for responsive and mobile-friendly web apps.

Updated on: 22-Nov-2023

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements