0

I found out how to add a "dummy" entry as the first "pseudo-option" in a select element here, but now I want to set certain elements to default values in the select element.

Is this something best done using javascript, or can I do it right after the dynamic population of options in the select, which is like so:

<label>From</label>
<select name="produsagefrom">
    @for (int i = 1; i <= @maxMonthsBackBegin; i++)
    {
        <option id="selItem_@(i)" value="@i">@i</option>
    }
</select>
<label>months back</label>
<label>To</label>
<select name="produsageto">
    @for (int i = 1; i <= @maxMonthsBackEndNormal; i++)
    {
        <option id="selItem_@(i)" value="@i">@i</option>
    }
</select>
<label>months back</label>

After populating the "produsagefrom" select, I want to set it to "13" and "produsageto" to "1" as default values.

Is this a job for jQuery, can I do it right there with razor, or is it possible to do it in a code-behind (C#) file?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

2

You could use JavaScript or Razor.

Here is Razor:

<select name="produsageto">
    @for (int i = 1; i <= @maxMonthsBackEndNormal; i++)
    {
        if(i == 1) {
            <option id="selItem_@(i)" value="@i" selected="selected">@i</option>
        } else {
            <option id="selItem_@(i)" value="@i" >@i</option>
        }
    }
</select>

Here is jQuery:

$('select[name=produsagefrom]').value = '13'
hvaughan3
  • 10,955
  • 5
  • 56
  • 76