placeholder text for chosen plugin for single select not working

非 Y 不嫁゛ 提交于 2019-12-05 00:26:12

Okay. I found out the answer. We need to have the first option as blank for the placeholder text to be activated for Single select search box.

<select id="search_pi" name="search_pi" type="text" class="chosen-select" style="width:450px;" onchange="this.form.submit()" >

  <option> </option>
  <?php
    include "../includes/search_dropdown.php";
    foreach($proj_ini_Array as $qa){
      echo "<option value = \"$qa\">$qa</option>";
    }
  ?>

In the above case, placeholder text will default to "Select an option".

For a customized placeholder-text for a single search select box you can edit your js file as below and have the first option blank for the placeholder text to be displayed:

<script>
  $(document).ready(function() {
    $('.chosen-select').chosen({
      placeholder_text_single: "Select Project/Initiative...",
      no_results_text: "Oops, nothing found!"
    });  
  });
</script>
Giridhar Shukla

use data-placeholder="YOUR TEXT" in select tag

To make your first option simply a guidance text, but unable for users to select it, use this:

<option selected="selected" disabled>Search Project/Initiative...</option>

* Edit *

Ah sorry, didn't read your question clearly enough to begin with.

Apparently the first <option> needs to be an empty one. See this jsfiddle.

There's actually a better/easier solution to this problem if you are constructing via HTML rather than JS. You have to do two things.

  1. The first <option> in your <select> should be empty.
  2. You should include placeholder="Placeholder goes here" in your <select>. Apparently this was once called data-placeholder, which I think has led to some confusion.

So, in total:

<select placeholder="Placeholder goes here">
    <option><!-- Empty option goes here. --><option>
    <option>Other Option</option>
</select>

User response @Zack Joerdan solved my problem. So the spring required value inside option

<select placeholder="Placeholder goes here">
    <option value=""><!-- Empty option goes here. --><option>
    <option>Other Option</option>
</select>

You can just simply add data-placeholder="YOUR TEXT" No need to use jquery for the same

Here is the example

<select id="search_pi"  data-placeholder="Select Project/Initiative..." name="search_pi" type="text" class="chosen-select" style="width:450px;" onchange="this.form.submit()" >
              <?php
                include "../includes/search_dropdown.php";
                foreach($proj_ini_Array as $qa){
                  echo "<option value = \"$qa\">$qa</option>";
                }
              ?>
</select>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!