I have a HTML Listbox and I need to add values to it on the page load. I have tried to call a JS function on page load event on both <body>
tag and <select>
tag but it does not execute the the function.
<body onload='popListbox(<%=session.getAttribute("objNames")%>)'>
<select id="lstObjects" onload='popListbox(<%=session.getAttribute("objNames")%>)'>
If I try onclick
it executes fine but not in onload
event. Can someone help me with this.?
UPDATE: my JS function
function popListbox(objList){
var select = document.getElementById("lstObjects");
var objects = objList;
var objects_array = [];
for(var i in objects) {
if(objects.hasOwnProperty(i) && !isNaN(+i)) {
objects_array[+i] = objects[i];
}
}
for(var i = 0; i < objects_array.length; i++) {
var opt = objects_array[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
}