0

I have a JS class like below.

It's ok with limited drop down list content, but I want to dynamically generate the content when it comes to say 1 ~ 100.

How can I do that?

  new Dropdown('dropdown', { items: [
  { value: '1', name: '1', selected: true},
  { value: '2', name: '2'},
  { value: '7', name: '3'}
  ]});

Thanks,

2
  • 2
    There are no classes in JavaScript. And the next time please post what you've tried. Right now the question looks like you are just looking for someone to write the code for you - sure, it's code that's written in less than a minute but nevertheless, it's not a good thing and you won't learn anything from it. Commented Nov 2, 2012 at 3:22
  • Thanks for the comments and answer. I was clueless in writing the function initially, a direction on how to write it is also appreciated. Commented Nov 2, 2012 at 6:26

1 Answer 1

2

Simply create an arry and then add objects to it in a loop:

var items = [];

for(var i = 1; i <= 100; i++) {
    items.push({
        value: i,
        name: i
    });
}

new Dropdown('dropdown', {
    items: items
});

If you want to preselect an item, simply set the selected property conditionally:

    items.push({
        value: i,
        name: i,
        selected: i == 1
    });

This would preselect the first item for example.

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.