0

I'm trying to teach myself Javascript & jquery so am very new & bit rubbish so far. Im working on a little project in which I want to create a select list with the values created dynamically from an array of objects. This is a snippet of my objects:-

function to(name, seedGroup, seed, number) {
this.name = name;
this.seedGroup = seedGroup;
this.seed = seed;
}

var austria = new to("Austria", 3, 0);
var belgium = new to("Belgium", 1, 1);
var bosnia = new to("Bosnia & Herzogovinia", 2, 2);
var bulgaria = new to("Bulgaria", 4, 3);

I want the value to be the object & the text to be the object.name. I have tried many different ways to achieve this & have tied myslef up in knots. Any help would be appreciated.

1 Answer 1

1

Something like this http://codepen.io/anon/pen/gADam?

function to(name, seedGroup, seed, number) {
this.name = name;
this.seedGroup = seedGroup;
this.seed = seed;
}

var countries = [
  new to("Austria", 3, 0),
  new to("Belgium", 1, 1),
  new to("Bosnia & Herzogovinia", 2, 2)
];

var options = '';
for (var i = 0; i < countries.length; i++) {
   options += '<option value="'+JSON.stringify(countries[i])+'">'+countries[i].name+'</option>';
} 

$('select').html(options);
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.