Clear select element with Javascript

Asked

Viewed 2,032 times

2

I have this select field:

<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 at the time you clicked, I needed the field to be ready to be typed. How can I do it? I’ve tried some ways, but without success. This is the last way I tried, but it didn’t work either:

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

Follows how I load txtpesquisa.

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();
}

  • 1

    Your query was not very clear, you want to clear which field, is using plugins?

1 answer

2


When you use the Selectize.js he hides his input and creates a number of elements (Divs and another input) to create the inbox for tags and options. This means that it is not advisable for you to work directly with input original, but rather with the object that is returned through the method selectize().

For you to listen when the object takes focus and clear 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 placeholder smarter. See below:

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

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

  • It didn’t work, I already have the search here, that the value is 0 and does the search if all, but I need that at the time the user click, clear the field so that it can type.

  • Did you use the first option to clean? Error occurred?

  • No, it does not occur any error, I will show you how to load txtpesquisa, I will update the question.

  • Any @bio tips ?

  • If the plugin is working correctly as in the first code you put in your question, the first solution I gave you, based on javascript code, should work. You reported that it does not work, but no error occurs, so what exactly is not working?

  • See here the events running. Including the onFocus, that runs as soon as the element takes focus.

  • In this other link there is an alternative way to capture the onclick, but it doesn’t seem so right to use it this way. Anyway, if I can help you...

  • 1

    I managed using your same code, $select[0].selectize.clear(); I only put this part, and it worked, thank you.

  • I’m glad I helped you and it worked, good luck! :)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.