views:

65

answers:

4

Typically when you need to select an item by default, you do:

<select>
  <option value="1">                 Volvo  </option>
  <option value="2" selected="true"> Saab  </option>
  <option value="3">                 Mercedes  </option>
  <option value="4">                 Audi  </option>
</select>

Is it possible to get something like this?

<select selectedValue="2">
  <option value="1">  Volvo  </option>
  <option value="2">  Saab  </option>
  <option value="3">  Mercedes  </option>
  <option value="4">  Audi  </option>
</select>

It works out easier in PHP since you only have to soft-code one value, instead of handling the selected attribute on any possible <option/>.

A: 

Put JavaScript after the declaration of the listbox and set the selected index there:

<script>
document.getElementById('listBoxId').selectedIndex=<?php echo $INDEX ?>;
</script>

Something like this.

dmitko
Could someone seriously explain to me why using JavaScript would be a good (up-voteable) solution in this case? (The explanation "I don't care for users without JS" is **not** valid)
RoToRa
I'm not saying that this is an ideal solution, but it could be a good workaround. The best of course would be generating html on the server side as explained in the most popular answer. Sometimes web applications are to be used in intranets and it can be assumed that JS is on by policy. Anyway thank you for pointing out that JavaScript works only if it's enabled.
dmitko
Its the best workaround, agreed. The other solutions simply reiterate the cause of the problem as an answer, and expect me to applaud them. Absolute rubbish.
Jenko
@Jenko I did answer your question, the answer was no. The point I was trying to make is that if your output is in a loop, it is only defined in one place as it is, so where is the problem?
roryf
I can't believe someone would consider this a good, or even the best workaround, especially since a simple function such as roryf's answer would solve the problem perfectly.
RoToRa
+3  A: 

There is no attribute like that on the <select> element. Assuming your <option> output is in a loop, I don't see how it makes a huge difference:

$selected = "2";
foreach($values as $key => $val) {
    echo "<option value=\"" . $key . "\"" . ($key == $selected ? " selected=\"selected\">" : ">") . $val . "</option>";
}

(my PHP is a little rusty, that may not be 100% correct)

roryf
+1 since this is near to what i had in mind
corroded
And if you put that in a reusable function, you never have to write it again.
RoToRa
A: 

No, but you can add your default value to your id like

<select id="default-value-2">

then in your options, you have

<option value="2" <?php echo is_this_the_default_value ? selected='true' : '' ?>

or something to that effect(forgive me i forgot my php syntax, but i hope you get the point).

Anyway, that is a dirty fix also, so i suggest just adding a method to check for the default selected tag and printing it selected="selected" when it is the default. You can just call it once if you loop through your select options

corroded
That won't work since it's the presence of the `selected` attribute that infers 'selectedness', irrespective of it's value.
roryf
oh sorry typo, you can just put the selected inside the condition then like so *see edit*
corroded
A: 

unfortunately, no. it is not possible =/

hugo_leonardo