0

This is the code I have - this turns the first array element but I need to increment through each array element rather.

<div class="row-fluid">
  <div class="control-group">
    <div class="controls">     
        <% _.each(answer_options, function(a, adx){ %>
        <label>
            <% if(question_type == '') { %>
            <input type="checkbox" name="form-field-checkbox" class="ace-checkbox-2"><span
                class="lbl"> <%= answer_options._answer %><%= answer_options[0].a_option %></span>
            <% } else { %>
            <input type="radio" name="form-field-radio"><span class="lbl"><%= answer_options[0].a_option %>&nbsp;<%= answer_options[0].answer %></span>
            <% } %>
        </label>
        <% }) %>
    </div>
</div>

3
  • 1
    no this is not javascript Commented Oct 21, 2013 at 22:42
  • please flag the template language Commented Oct 21, 2013 at 22:44
  • is this EJS that you are using? Commented Oct 21, 2013 at 22:51

1 Answer 1

1

The each function iterates through each value in answer options, but you're always selecting the first value from the array by doing answer_options[0].a_option instead of doing a.a_option. The each function gives a and adx where a is the value and adx is the index where that value lives.

<% _.each(answer_options, function(a, adx){ %>
   <label>
       <% if(question_type == '') { %>
          <input type="checkbox" name="form-field-checkbox" class="ace-checkbox-2"><span
                class="lbl"> <%= a._answer %><%= a.a_option %></span>
            <% } else { %>
          <input type="radio" name="form-field-radio"><span class="lbl"><%= a.a_option %>&nbsp;<%= a.answer %></span>
       <% } %>
    </label>
<% }) %>
Sign up to request clarification or add additional context in comments.

1 Comment

@user2904967 mark this as the answer if it answers your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.