Custom Fields
Re-Envisioned
Support General Get selected option in select field and display

  • Creator
    Topic
  • #29777
    Kirb
    Participant

    Hi, How can I add text/label "category" to the selected option value but hidden in the list of dropdown select?

    The option should look like this:
    Output we want: Category: Taxonomy Name

    <select>
       <option>text 1</option>
       <option>text 2</option>
       <option>text 3</option>
    </select>

    Right now:

    <select>
       <option>category: text 1</option>
       <option>category: text 2</option>
       <option>category: text 3</option>
    </select>
    <div class="nm-category-filter mobile-category">
     <select data-store-id="{{ post.ID }}" class="getcatdishes" id="mobile-select">
       {% for clone in post.add_menu %}
         <option value="{{ clone.add_taxonomy.id }}">category: {{ clone.add_taxonomy.name }}</option>
       {% endfor %}
     </select>
    </div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #29807
    Long Nguyen
    Moderator

    Hi Kirb,

    You can use the JavaScript code to append text to the selected option. Please refer to this topic https://stackoverflow.com/questions/43444376/how-do-i-append-text-to-the-end-of-a-select-options-text-using-jquery/43444656

    #29811
    Kirb
    Participant

    Hi, I tried it but this selected multiple options when I select it.

    <script>
    jQuery(document).ready(function(){
         var current_text = jQuery("#mobile-select :selected").text();
         jQuery('#mobile-select').change(function() {
         jQuery(this).children(':selected').text(current_text + " (current)");
       });
    });
    </script>

    Result

    <select id="mobile-select">
    <option>test 1 (current)</option>
    <option>test 2 (current)</option>
    <option>test 3 (current)</option>
    <option>test 4 (current)</option>
    </select>

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.