Reputation: 50732
I am passing the below to my Underscrore template (now fieldAttr is dynamic...can have 2 attrs, 3 attrs, etc)
fieldAttr: {
id: "myElmId",
dataModelId: $(this).attr('data-model-id')
}
Now in my HTML template, I want to loop and set finalAttributesString (which i'll use to set my textarea attributes)
<% for (var i = 0, i < fieldAttr.length; i++) { %>
//logic to get finalAttributesString
<% } %>
<textarea <%=finalAttributesString%>></textarea>
So it should render as below:
<textarea id="myElmId" data-model-id="123"></textarea>
Can I do this using Underscore JS?
Upvotes: 3
Views: 139
Reputation: 2405
It much easier to do this
<textarea <% for(var attr in fieldAttr) { %> <%= attr %>="<%= fieldAttr[attr] %>" <% } %>
></textarea>
I made a jsfiddle here
Upvotes: 1