Convert a List into a Select Dropdown using jQuery | HTML Goodies

Convert a List into a Select Dropdown using jQuery

Written By
RG
Rob Gravelle
Apr 13, 2015
4 minute read

Convert a List into a Select Dropdown using jQuery

Look around the Internet and you’ll find lots of JavaScript in-place editor libraries that allow you to convert display text into an editable field. Just as interesting, but less common, is the select-in-place list. That works much the same way as in-place text editing, but allows the user to select from an ordered or unordered list by converting it into a dropdown lends itself very well to voting polls. Say that you had a list of fruits. You could decide to submit your own choice by clicking on it. That might be a frivolous thing to be voting on, but in a world where the color of a dress can break the Internet, why not?

Depending on whether we wanted to list the items in order of importance, we could use either an ordered or unordered list. Both have exactly the same syntax, except for the outer tags, which are

    and
      respectively. Here is the markup and resulting lists produced by both tags:

      1. What’s your favorite fruit?
      2. apples
      3. oranges
      4. bananas
      5. strawberries
      6. raspberries
      7. melons
      8. grapes
      9. tangerines
      • What’s your favorite fruit?
      • apples
      • oranges
      • bananas
      • strawberries
      • raspberries
      • melons
      • grapes
      • tangerines
      <UL class="dropdown">
        <LH>What's your favorite fruit?</LH>
        <LI>apples</LI>
        <LI>oranges</LI>
        <LI>bananas</LI>
        <LI>strawberries</LI>
        <LI>raspberries</LI>
        <LI>melons</LI>
        <LI>grapes</LI>
        <LI>tangerines</LI>
      </UL>
      • What’s your favorite fruit?
      • apples
      • oranges
      • bananas
      • strawberries
      • raspberries
      • melons
      • grapes
      • tangerines

      Did you know that lists support the tag? It’s short for List Header and acts much like the Table Header tag. It semantically adds a heading to a list.

      Referencing jQuery

      As alluded to in the title, we’re going to use jQuery to generate the by passing in the tag name. The replaceWith() function instantly swaps the list with the new element. The option text is retrieved from the list element and set on the option using the html() function. The value attribute is set using the index argument that is passed to the each() function. Feel free to substitute a more meaningful value for your application.

       var $list   = $(this),
           $select = $('<select />');
      
      $list.children('li').each(function(index) {
        $select.append($('<option />').attr('value', index).html($(this).html()));
      });
      
      $list.replaceWith($select);

      Giving the

HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.