4

In Javascript I have a User object that contains an array of department objects like so:

user = 
{
   departments: [
        {id: 1, name: 'Department 1'},
        {id: 2, name: 'Department 2'},
        {id: 3, name: 'Department 3'}
   ]
}

Using jquery templates (version 1.4.2) I want to render a <select> tag like so:

<select name="departmentId" id="department">
    {{each departments}}
        <option value="{{= id }}">{{= name }}</option>
    {{/each}}   
</select>

This doesn't produce any values in the option elements. How do access the id and name properties of each of the department objects with the template.

2
  • 2
    "Doesn't work" is never a sufficient problem description. Commented Apr 1, 2012 at 16:31
  • 1
    Amended question so it no longer says "doesn't work" but explains that the code doesn't produce values in the option elements Commented Apr 1, 2012 at 16:37

1 Answer 1

7

Use it like

<select name="departmentId" id="department">
{{each departments}}
    <option value="${idField}">${textField}</option>
{{/each}}</select>
Sign up to request clarification or add additional context in comments.

Comments

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.