Erik van Brakel
Erik van Brakel

Reputation: 23840

Default value for <select> tags

I've got a select with two 'special' options, a bit like this:

<select>
 <option value="????">Choose one:</option>
 <option value="1">option1</option>
 <option value="2">option2</option>
   ....
 <option value="????">Free input</option>
</select>

When the user selects nothing, I should ignore the input. When the user selects 'free input', the value from a corresponding textbox is used.

I was wondering, which values would you give those options? I figured I should be able to use no value for the first, because that's what it is: no value. But the last option, should I use -1, 0, or something different?

Upvotes: 1

Views: 2038

Answers (6)

Tomalak
Tomalak

Reputation: 338416

If the rest of your options has numeric values, choose non-numeric values with "speaking values" for the special options.

This way the server logic can decide with a simple IsNumeric() call what to do, and you are free to add other special options later on.

Upvotes: 2

Kim
Kim

Reputation: 2787

Make the first option an option group instead and then use no value for your "Free input". An option group is not selectable.

Upvotes: 0

Evan Fosmark
Evan Fosmark

Reputation: 101811

On the first one, you really don't even need the value attribute present. It'll come up as not being set on the server side regardless.

With regards to the free input choice, I'd go simply with free_input.

Of course, it's all up to you. Just choose something that is meaningful and relates to its purpose. ;-)

Upvotes: 0

Yaakov Ellis
Yaakov Ellis

Reputation: 41560

How are you processing your data?

In C#, I will define a constant to represent the ID, and use this to identify it.

public string CONSTANT_OPTION_FreeInput = "-1";

// Test for value in a different method:
if (selectedValue == CONSTANT_OPTION_FreeInput) {
  // Do Stuff
}

You can do something similar in other languages as well.

Whatever value you use, just make sure that it will be easily understood by yourself and any other developer looking at the code in the future.

Upvotes: 0

Sebastian Dietz
Sebastian Dietz

Reputation: 5706

Why not use "free" and "none". You are not restricted to integer values, so using descriptive values would improve comprehensibility.

Upvotes: 1

Boris Pavlović
Boris Pavlović

Reputation: 64650

I would use something that will be understandable to the next developer who will be reading the code. Therefore something like 'freeInput' or 'textFieldLookupValue' could be appropriate

Upvotes: 2

Related Questions