Clear select element with JavaScript

2

I have this field select:

<div class="control-group">
  <select runat="server" onclick="ClearOptions(this);" name="txtpesquisa" id="txtpesquisa" class="form-control.selectize-control">
  </select>
  <script>
    $('#txtpesquisa').selectize();
  </script>
</div>

I need to clean it by clicking, where it opens the search, because it is written search here, then when it clicked, I needed the field ready to be typed. How can I do it? I have tried some forms, but to no avail. This is the last form I tried, but it did not work either:

function ClearOptions(id) {
  document.getElementById(id).options.length = -1;
}

Here's how I load the search.

private void CarregaPessoa() {

  SqlCommand comando = new SqlCommand();
  comando.Connection = clsdb.AbreBanco();
  comando.CommandType = CommandType.Text;
  comando.CommandText = "select id, nome, classificacao_id from PESSOA where id != 0 order by nome asc";

  SqlDataAdapter da = new SqlDataAdapter();
  da.SelectCommand = comando;
  DataSet ds = new DataSet();
  da.Fill(ds);

  DataRow dr = ds.Tables[0].NewRow();
  dr[0] = 0;
  dr[1] = "Pesquise aqui .. ";
  ds.Tables[0].Rows.InsertAt(dr, 0);

  txtpesquisa.DataSource = ds.Tables[0];
  txtpesquisa.DataTextField = "nome";
  txtpesquisa.DataValueField = "id";
  txtpesquisa.DataBind();
}
    
asked by anonymous 08.05.2018 / 22:45

1 answer

2

When you use Selectize.js it hides your input and creates a series of elements (divs and another input) to create the inbox for the tags and options. This means that you are not advised to work directly with the% original%, but rather with the object that is returned by the input method.

For you to listen when the object takes focus and clean it, use as below:

var $select = $('#txtpesquisa').selectize({
    onFocus : function(){
        $select[0].selectize.clear();
    },
});

However, I think you can use it in a better way. What you are looking for may be a% smarter% s. See below:

var $select = $('#txtpesquisa').selectize({
    placeholder : "Pesquise aqui"
});

Here you can find more information on how to use Selectize. js and you can see the rest of your settings.

    
08.05.2018 / 23:22