How to define a selected item in ranged select/options

I use code

<select class="order form-control border-0" id="select-1" required>
{{ range (index .Site.Data .Params.supplier .Params.ramtype) }}
	<option value="{{ .price }}" selected>{{ .item }}</option>
{{ end }}
</select>

to range items and prices from the list

DDR3:
  - item: Select RAM
    price: 
  - item: DDR3 2GB
    price: 12.00
  - item: DDR3 4GB
    price: 19.00
  - item: DDR3 8GB
    price: 33.00

What should I do if I want one of the items selected? I’d read, but AFAIK this is not documented. I tried

  • item{selected}: DDR3 8GB - does not work
  • item[selected]: DDR3 8GB - does not work
  • item(selected): DDR3 8GB - does not work

Please advise!

There is no such thing in your template that parses something and sets the selected attribute from that. It just echoes selected always. You could do the following:

DDR3:
  - item: DDR3 2GB
    price: 12.00
    selected: true
  - item: DDR3 4GB
    price: 19.00

and in the layout:

<option value="{{ .price }}" {{ with .selected }}selected{{ end }}>{{ .item }}</option>

Thank you, It works! Great!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.