HTML Select Placeholder
Update
Adding a selected and disabled attribute works as well and doesn't require any javascript, however refreshing the page does not reset the value. Doing a hard refresh does reset the value (for firefox). However there seems to be a placeholder attribute in draft (as of november 2014). "When there is no default option, a placeholder can be used instead". However in the example the placeholder value is still selectable after a choice has been made. This may not be what you want, so combining the two attribute and the javascript below guaranty the behavior I'm looking for. I could even support older browser with a little work.
The Purpose of this article is to make a placeholder in select that should help prevents bad selections with and make it easier to validate if no selection is made. In this article I will assume you have Jquery and the Jquery Validation plugin installed on your web application and similar html code displayed like the the following...
Example: http://jsfiddle.net/2TcnL/15/
Note:
With "Jquery.Validate.Unobtrusive.js" the disabled option activates the jquery validation and anything else disabled since it is an invalid option. So if your using Asp.net MVC and have both js libraries installed than your done here!
You can find tons of different solutions but I like this way more since it uses everything out of the box. No extra styling required and just a little jquery for validating which you can customize for your situation.
Questions (Things I asked myself)
Why didn't I just use "disabled" with "selected" inside the html option tag? On some browsers it showed that this would simply remove the default option and set the next option in line.
What happens if I set all the options to disabled? This would set the default option disabled and the selected option but the other options would be disabled as well, close to the behavior we wanted.
Is Jquery.Validate.Unobtrusive.js something Microsoft specific? I was under the assumption that it wasn't but the file is actually created by Microsoft. Good thing to learn since I use it so much with Asp.net MVC.
Reference:
Microsoft Ajax Content Delivery Network - ASP.NET Ajax Library
Resource
4.10.9 The select element — HTML5
Adding a selected and disabled attribute works as well and doesn't require any javascript, however refreshing the page does not reset the value. Doing a hard refresh does reset the value (for firefox). However there seems to be a placeholder attribute in draft (as of november 2014). "When there is no default option, a placeholder can be used instead". However in the example the placeholder value is still selectable after a choice has been made. This may not be what you want, so combining the two attribute and the javascript below guaranty the behavior I'm looking for. I could even support older browser with a little work.
The Purpose of this article is to make a placeholder in select that should help prevents bad selections with and make it easier to validate if no selection is made. In this article I will assume you have Jquery and the Jquery Validation plugin installed on your web application and similar html code displayed like the the following...
<select> <option value="">Please make a selection...</option> <option value="1">A Option</option> <option value="2">B Option</option> </select>
Html Result (try it out)
So an example would be the user is making a selection from the dropdown, but they cancel this by clicking outside the dropdown. The user can cancel by other means but in all cases this would make the "Please make a selection" option the user's choice. Now you could validate that the value of the option or prevent the from selecting it and notify the user that they must make a selection. In my approach I prefer to do both, avoid them from selecting the option and validating to make sure they have made a selection.Example: http://jsfiddle.net/2TcnL/15/
$('select option:first').attr('disabled', true);
Note:
With "Jquery.Validate.Unobtrusive.js" the disabled option activates the jquery validation and anything else disabled since it is an invalid option. So if your using Asp.net MVC and have both js libraries installed than your done here!
You can find tons of different solutions but I like this way more since it uses everything out of the box. No extra styling required and just a little jquery for validating which you can customize for your situation.
Questions (Things I asked myself)
Why didn't I just use "disabled" with "selected" inside the html option tag? On some browsers it showed that this would simply remove the default option and set the next option in line.
What happens if I set all the options to disabled? This would set the default option disabled and the selected option but the other options would be disabled as well, close to the behavior we wanted.
Is Jquery.Validate.Unobtrusive.js something Microsoft specific? I was under the assumption that it wasn't but the file is actually created by Microsoft. Good thing to learn since I use it so much with Asp.net MVC.
Reference:
Microsoft Ajax Content Delivery Network - ASP.NET Ajax Library
Resource
4.10.9 The select element — HTML5