Show blank value in html select element

I have an HTML selector (combo box) in a form with a pair, if it has options. As always, each parameter has a specific meaning. During page initialization, I get a value from the server, which I should use as the selected parameter in the list. Sometimes, however, I get a value from a server that does not match any of the available parameters. (Grrrr)

I want to inform the user that the value received from the server is not valid. What I would like to do is to show an empty selection field (as if there was no choice) without the actual empty option as one of the parameters. I would also like to use the default select item, if possible. Is this possible?

Edit: When you set the combox value to 'IE9 (I used $ (' select '). Val (' ')), it empties the text in the combo box, which is exactly what I need. Unfortunately, only IE9 does this, so this is not an option.

+4
source share
2 answers

Due to the selection, HTML selection does not provide such functionality. You will need to add an empty <option value="somethingwrong">Please, pick a value</option> element and use scripts to check if the user has selected this particular value. I suggest catching both the onchange event of the dropdown menu and the onsubmit event of the whole form.

+4
source

You can insert an option into select and remove it when it is changed to reduce problems with these parameters.

Check out the demo here

+1
source

Source: https://habr.com/ru/post/1413766/


All Articles