HTML selection option with EJS

I am creating a configuration for my web application, trying to transfer it to a web page. The following is part of my code. I want the option to be selected in config [0] .volume has. So, if config [0] .volume is '50', the selected option will be '50'. Codes work well. But I thought. "How can I shorten this code?" So many details with my code.

<select id="volume"> <option value="1" <%= config[0].volume == '1' ? 'selected' : ''%>>1</option> <option value="5" <%= config[0].volume == '5' ? 'selected' : '' %>>5</option> <option value="10" <%= config[0].volume == '10' ? 'selected' : '' %>>10</option> <option value="50" <%= config[0].volume == '50' ? 'selected' : '' %>>50</option> <option value="75" <%= config[0].volume == '75' ? 'selected' : '' %>>75</option> <option value="100" <%= config[0].volume == '100' ? 'selected' : '' %>>100</option> </select> 

I think about 2 hours, but nothing works. Maybe I need to use jquery or javascript, and add an attribute to it,

 attr('selected')... ? 

I have no idea if you could help me ..?

+6
source share
1 answer

You can put it in a loop based on parameter values.

 <select id="volume"> <% var options = [ "1", "5", "10", "50", "75", "100" ]; for ( var i = 0; i < options.length; i++ ) { var selected = ( config[0].volume == i ) ? "selected" : ""; %><option value="<%=options[ i ] %>" <%=selected %>><%=i %></option><% } %> </select> 
+9
source

All Articles