0

I have an empty drop down list in HTML. I am trying to add new attribute into the list using .createElement('option') method. However, the item i want to add has name, address, and phone number so how can i add,as an object, into drop down list? Thank you for reading it.

  • 2
    Have you considered adding a string representation of the object (e.g. `Joe Egan (San Diego, CA) @ 858-876-5309`)? – J0e3gan Jul 28 '15 at 23:59
  • So this is what have for now – Chaudhry Khan Jul 29 '15 at 01:27
  • function additem(name, address, phone){ var opt = document.createElement("option"); var sel = document.createElement("droplist"); //now i have three attributes name, address and phone, and i want to make an object which will consist of these three attributes sel.add(opt,"i want to add object but only show its name in the list"); } – Chaudhry Khan Jul 29 '15 at 01:29
  • Could found your answer [here](http://stackoverflow.com/questions/3245967/can-an-option-in-a-select-tag-carry-multiple-values). Display only a text with multiple value option. – Neo Jul 29 '15 at 02:30

1 Answers1

0

Im not sure if this is what you wanted, I got it from https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/add

var sel = document.getElementById("existingList");

var opt = document.createElement("option");
opt.value = "3";
opt.text = "Option: Value 3";

sel.add(opt, sel.options[1]);

[Edit]

Im pretty tierd and the code is not the best. Maybe you can play around with it and see if it fits your code.

var sel = document.createElement("droplist"); 
var opt1 = document.createElement("OPTION");
var opt2 = document.createElement("OPTION");
var opt3 = document.createElement("OPTION");

var information = {
  names: ["nameOne", "nameTwo"],
  addresses: ["adressOne", "adressTwo"],
  names: ["phoneOne", "phoneTwo"],
  informationFunction: function additem(){ 
    opt1.value = "1";
    opt1.text = this.addresses[0];    
    opt2.value = "2";
    opt2.text = this.addresses[1];    
    opt3.value = "3";
    opt3.text = this.addresses[3];
    sel.appendChild(opt1);
    sel.appendChild(opt2);
    sel.appendChild(opt3);
  } 
};
information.informationFunction();
Modig
  • 985
  • 2
  • 9
  • 20
  • function additem(name, address, phone){ var opt = document.createElement("option"); var sel = document.createElement("droplist"); /**now i have three attributes name, address and phone, and i want to make an object which will consist of these three attributes**/ sel.add(opt,"i want to add object but only show its name in the list"); } – Chaudhry Khan Jul 29 '15 at 01:25