Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select box with first option empty

Does anybody know how can I set in my select box the first option to empty value?

I'm getting the data from my DB, and I would like to set the option by default as "Please select one option".

like image 218
JaviZu Avatar asked Jul 23 '14 13:07

JaviZu


People also ask

How do you keep a select box blank in HTML?

Just put "hidden" on option you want to hide on dropdown list.

How do I make first value default dropdown?

The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute.

What does it mean when the value of the selected menu option is an empty string in Javascript?

</option> , value="" is turned into just value . The value, when set to the empty string is simply discarded.


1 Answers

I found that 'default'=>'Please select' doesn't work with the HTML5 required attribute. This does work:

$listOfValues = [1 => 'Choice 1']; Form::select('fieldname',[null=>'Please Select'] + $listOfValues); 

If you don't like modern PHP syntax,

$listOfValues = array(1 => 'Choice 1'); $listOfValues[null] = 'Please Select'; Form::select('fieldname', $listOfValues); 

But the point is to have a label for the null value.

like image 104
darkbluesun Avatar answered Oct 02 '22 02:10

darkbluesun