Avadhani Y
Avadhani Y

Reputation: 7626

JQuery - Unable to add SELECT options dynamically

I have a problem with adding OPTIONS to SELECT tag dynamically.

Here is my code of Job.html:

<body onload="javascript:onPageLoad();">
<div id="JobForm" data-role="page"> 
    <div class="logoHeader"><img src="images/header.png" /></div> 
    <div data-role="header" data-position="fixed">
        <h1>Load<span id="JobHeader"></span></h1>
    </div>  
    <div class="lineBreak20"></div>
    <div class="rightContent divContent">Welcome<em id="lblName"></em></div>
    <div class="lineBreak40"></div>
    <div class="boldText divContent">Please select the Job</div>
    <div class="lineBreak20"></div>
    <div class="roundBox">
        <table>
                <tr>
                <td class="tdLabel">Job No: </td>
                <td><select id="ddJNo" class="fieldWidth" data-mini="true"></select>
                </td>
            </tr>
            <tr>
                <td></td>
                <td><button id="btnLd" type="submit" data-theme="a" data-inline="true" data-mini="true">Load</button></td>
            </tr>
         </table>
    </div>
</div> 
   </body>

and the Javascript is:

 function onPageLoad() {
var lsJobs= window.localStorage.getItem("jobs");
var arrJobs= lsJobs.split(",");
for (var i=0;i< arrJobs.length;i=i+2)
{
    if((arrJobs[i]!=""))
    {
    $("#ddJNo").append("<option value='"+ arrJobs[i] +"'>" + arrJobs[i+1] + "</option>");
    }
}
document.getElementById("lblName").innerHTML =window.localStorage.getItem("name");
 }

Is there any error in the code? Here the problem is when i click the Select option, the drop-down list is not showing. Please share ur ideas in implementing the SELECT options dynamically either using JAVASCRIPT or JQUERY.

UPDATE: But while placing the window.localStorage content in alert Dialog, it works fine. I think there is a problem with JS Function. Please help me...

Upvotes: 1

Views: 3441

Answers (1)

Robin Maben
Robin Maben

Reputation: 23034

Try this,

var newOption = $("<option />");
$("#ddlJobNo").append(newOption);

newOption.val(arrJobs[i]); 
newOption.html(arrJobs[i+1]); 

See example here.

Upvotes: 2

Related Questions