How to use chain.js to append select options?

4 views
Skip to first unread message

Alan She

unread,
Nov 3, 2008, 11:06:32 PM11/3/08
to chainjs
I would like to know if I can use chain.js to append select options?

What I want to do is that I have a piece of html as follow,

<select id="categories" name="categories">
<option class="category" value="xxx">XXX</option>
</select>

And I have a list of JSON object like

[
{id:1,name:'aaa'},
{id:2,name:'bbb'},
{id:3,name:'ccc'},
]

I tried to use chain in this way,

$('#categories').items(jsonObject).chain({
'.category' : {
value: '{id}',
text: 'name'
}
});

It doesn't work. The <option> element are copied 3 times but the value
and text are not bound on it.
But the above usage is similar to the way I use for <div> or <span>
elements.
I wish to have a result like this,

<select id="categories" name="categories">
<option class="category" value="1">aaa</option>
<option class="category" value="2">bbb</option>
<option class="category" value="3">ccc</option>
</select>

How can I do this with chain.js?
Thank you very much!

Rizqi Ahmad

unread,
Nov 4, 2008, 11:11:46 AM11/4/08
to chainjs
Hi Alan,

you can't access the <option> using '.category' because it is the
parent object.

in principle you only have to change '.category' to self. This feature
is new in v0.2

you can see an example of it in demo folder.
http://github.com/raid-ox/chain.js/tree/master/demo/chain-self.html
Reply all
Reply to author
Forward
0 new messages