Revision: 37902
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 20, 2010 05:39 by nomada
Initial Code
/** @param objSelect an object returned by document.getElementById('myselect') or your favourite library function. @param arrDatos is an array or an array of arrays. @param numCurOption is the key of the option we need to be SELECTED. @param boolEmpty should be true if we need a first element without value and label. */ function array2select(objSelect, arrDatos, numCurOption, boolEmpty) { var elOptNew; objSelect.length = 0; if(boolEmpty) { elOptNew = document.createElement('option'); elOptNew.value = elOptNew.text = ''; addOption(objSelect, elOptNew); } for(var id in arrDatos) { if(typeof(arrDatos[id]) == 'object') { var arrResultado = arrDatos[id]; var strValue = arrResultado.ID; var strText = arrResultado.LABEL; } else { var strValue = id; var strText = arrDatos[id]; } elOptNew = document.createElement('option'); if(numCurOption == strValue) { elOptNew.selected = true; } elOptNew.value = strValue; elOptNew.text = strText; addOption(objSelect, elOptNew); } }
Initial URL
Initial Description
This function automates the creation of the <OPTION>s of a <SELECT> element. It's ideal for your AJAX apps when you create display elements using Javascript It accepts to different kind of arrays: An array of key:values… var arrDirs = { "a1":"Sede central", "a2":"EnvÃos", "a4":"Facturación", "b1":"Sucursal" }; An array of arrays, where each of the child arrays have the sintax 'ID': value, 'LABEL': text… var arrDirs = { "0":{"ID":"a1","LABEL":"Sede central"}, "1":{"ID":"a2","LABEL":"EnvÃos"}, "2":{"ID":"a4","LABEL":"Facturación"}, "3":{"ID":"b1","LABEL":"Sucursal"} }; Note that the ID part can be a number or an string.
Initial Title
Array to SELECT
Initial Tags
javascript, object, array
Initial Language
JavaScript