Creating sub options in a html dropdown <select>

Sometimes it is good to create sub options of an HTML drop down list <select></select>

For that purpose, <optgroup></optgroup> tags are used. Here is an example:

<form id="form2" name="form2" method="post" action="">

<select id="ddlcountries" name="ddlcountries">
    <option>--Select City--</option>
    <optgroup label="America">
        <option>United States</option>
        <option>Brazil</option>
        <option>Bostwana</option>        
    </optgroup>
    
    <optgroup label="Asia">
        <option>Pakistan</option>
        <option>Afghanistan</option>
        <option>China</option>
    </optgroup>
    
    <optgroup label="Africa">
        <option>South Africa</option>
        <option>Sudan</option>
        <option>Ethopia</option>
    </optgroup>
</select>
</form>

 

Add a Comment

Your email address will not be published. Required fields are marked *