How to create Optgroup selects using jQuery Mobile ?

jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be creating Optgroup selects using jQuery Mobile.

Approach: First, add jQuery Mobile scripts needed for your project.

<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>

Example:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <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>
</head>
  
<body>
    <center>
        <h1>w3wiki</h1>
  
        <h4>
            optgroup option Selects 
            using jQuery Mobile
        </h4>
    </center>
      
    <form>
        <div data-role="fieldcontain">
            <label for="Beginner">Select Element:</label>
            <select name="Beginner" id="Beginner">
                <option>Choose...</option>
                <optgroup label="Group 1">
                    <option value="1">Beginner1</option>
                    <option value="2">Beginner2</option>
                    <option value="3">Beginner3</option>
                    <option value="4">Beginner4</option>
                </optgroup>
                <optgroup label="Group 2">
                    <option value="5">Beginner5</option>
                    <option value="6">Beginner6</option>
                    <option value="7">Beginner7</option>
                </optgroup>
            </select>
        </div>
    </form>
</body>
  
</html>


Output:



Contact Us