Page 1 of 1

Why do you need value in <select>?

Posted: Wed Apr 16, 2008 11:25 am
by andrei.mita
Hello,

Had a very time consuming problem today. For some reason I don't know yet, my java script stopped reading values for <select> boxes in one of my forms. The other elements worked fine, only the value from the select was null.

Here is what I mean. I use a ciclyc function to read all the elements and values from a form. The function imports the value from the select only if I define the options like this: <option value="first">first</option>. If I do it like this: <option>first</option>, without value defined, it returns null.

Does anyone have an opinion?

Thanks,

Re: Why do you need value in <select>?

Posted: Wed Apr 16, 2008 12:44 pm
by John Cartwright
If you don't define a value, then what would you expect it to be? Of course it will be null ;)

(or did I misunderstand?)

Re: Why do you need value in <select>?

Posted: Wed Apr 16, 2008 9:18 pm
by JellyFish
The reason for the value attribute is to give the option element a value. The value of an option element should be in it's value attribute and no where else.

Code: Select all

<option value="[i]value[/i]">[i]text element[/i]</option>
The reason for the separation of the option element's value and it's textual appearance is for flexibility. For example, let's say you had the following:

Code: Select all

 
<select>
<option value="32">Joe's age</option>
<option value="23">Brian's age</option>
</select>
 
As you can see here that without the value attribute there would be no way to determine what age we want.

In Javascript we can get the value of an HTML element using the element.value property. But if you'd like the "textual stuff", you'd use element.innerHTML or element.textContent.

Re: Why do you need value in <select>?

Posted: Wed Apr 16, 2008 9:44 pm
by s.dot
I vaguely recall back when I used to use IE (ie6 i believe... maybe 7), that if you did not define a value, it would take the value of what's inside the option tag.

I could be wrong, but this is why I started making my selects have a default value, and then checking the values against expected values + the default value.

Re: Why do you need value in <select>?

Posted: Thu Apr 17, 2008 1:34 am
by andrei.mita
I get it now. Thank's for the info. It still is very odd why it worket for a while and why it still works if I take out the form from and run it alone.
Anyhow, thanks a lot for the explenation, it's good I discovered this "bug" in my application at this early stage of the project.