Why do you need value in <select>?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

Why do you need value in <select>?

Post 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,
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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?)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

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

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

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

Post 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.
Post Reply