Get selected option in select field and display

Support General Get selected option in select field and display

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #29777
    JamesJames
    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>
    #29807
    Long NguyenLong 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
    JamesJames
    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 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.