Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting an HTML option value to the empty string

Tags:

html

I have the following HTML code used to create on a page:

<select>
    <option value="" selected>test</option>
    <option value="test2">test2</option>
</select>

But when looking at the HTML in Google Chrome's DOM inspector it looks like this:

<select>
    <option value selected>test</option>
    <option value="test2">test2</option>
</select>

See the difference? For the first <option>...</option>, value="" is turned into just value. The value, when set to the empty string is simply discarded. Is there any way to set the value of an option tag to the empty string? I need this because I'm pulling elements out of a database to create a <select> menu. Each <option> will have it's value set to value of a database element and some of those elements have the empty string as their value.

like image 841
J-bob Avatar asked Jan 16 '13 05:01

J-bob


2 Answers

It is just Google Chrome's DOM inspector's notation (removes '=""' from attributes). If you inspect it with FireBug or simply view the source then you will see it is OK.

Your sample code produces this in Google Chrome:

Chrome's inspector removing the quotes and equal sign

And this is the result in FireFox:

FireBug showing that correctly

So don't worry about that.

like image 84
harry Avatar answered Sep 27 '22 21:09

harry


bob as Hanky said you can use selected = "selected" or "" so the code will be :

<select>
<option value="" selected="selected">test</option>
<option value="test2">test2</option>

or

<select>
<option value="" selected="">test</option>
<option value="test2">test2</option>

Checking this article may help : W3 option language reference

like image 31
Mohammed Sayed Avatar answered Sep 27 '22 22:09

Mohammed Sayed