Click here to Skip to main content
15,892,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Team,

I have the code as follows in jQuery.
HTML
<select id="batchIDSelect" class="combo100">
    <option>Select </option>
</select>

JavaScript
function GetDisplayBatchID() {
    debugger;
    $.ajax({
        url: 'QCAllocation.aspx/GetDisplayBatchID',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        success: function (result) {
            debugger;
            if (result.d != '') {
                if (result.d != '' && result.d != null) {
                    DisplayBatchIDList = JSON.parse(result.d);
                    var drawRow = function (batchID) {
                        var dropdown = document.getElementById('batchIDSelect');
                        for (var i = 0; i < batchID.length; i++) {
                            var opt = document.createElement('option');
                            opt.innerHTML = batchID[i].BatchId;
                            dropdown.appendChild(opt);
                        }
                    }

                    drawRow(DisplayBatchIDList);
                    manageOverlay(false);
                }
            }

        },
        error: function (error) {
            manageOverlay(false);
            alert(error.responseText);
        },
        async: false
    })
};


I have call GetDisplayBatchID() on page load. In This,I am getting "Select" value by default in DropDownList.I want to keep blank DropDownList when page load.

How it possible?
I think we need to modify the For Loop, will you show me how to find it ?
Please guide Me.

Thanks
Harshal

[Edit member="Tadit"]
Corrected formatting and grammatical issues.
Added pre tags.
[/Edit]
Posted
v3
Comments
Do you want to bind data and select the blank value at top or you don't want to bind anything, but show only a blank?
R Harshal 3-Apr-14 4:32am    
Thanks for you reply.
i want to bind the data but at top it should be blank.
See my answer. :)
R Harshal 3-Apr-14 4:31am    
Thanks for you reply.
i want to bind the data but at top it should be blank.
Sanjay K. Gupta 3-Apr-14 4:48am    
You can insert a blank item at position 0

Try like below...
JavaScript
var dropdown = document.getElementById('batchIDSelect');

var opt = document.createElement('option');
opt.innerHTML = ""
dropdown.appendChild(opt);

for (var i = 0; i < batchID.length; i++) {
    var opt = document.createElement('option');
    opt.innerHTML = batchID[i].BatchId;
    dropdown.appendChild(opt);
}

$("#batchIDSelect option[value='']").attr('selected', true);

Let me know, if it works or not.
 
Share this answer
 
Comments
R Harshal 3-Apr-14 5:11am    
Thank you So Much MR. Tadit
Its working ..
Thank you so much once again.
Harshal.
My pleasure. :)
You can insert a blank item at position 0
C#
function GetDisplayBatchID() {
    debugger;
    $.ajax({
        url: 'QCAllocation.aspx/GetDisplayBatchID',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        success: function (result) {
            debugger;
            if (result.d != '') {
                if (result.d != '' && result.d != null) {
                    DisplayBatchIDList = JSON.parse(result.d);
                    var drawRow = function (batchID) {
                     var dropdown = document.getElementById('batchIDSelect');
                     var opt = document.createElement('option');
                     opt.innerHTML ='';
                     dropdown.appendChild(opt);//insert blank
                      for (var i = 0; i < batchID.length; i++) {
                            opt = document.createElement('option');
                            opt.innerHTML = batchID[i].BatchId;
                            dropdown.appendChild(opt);
                      }
                    }

                    drawRow(DisplayBatchIDList);
                    manageOverlay(false);
                }
            }

        },
        error: function (error) {
            manageOverlay(false);
            alert(error.responseText);
        },
        async: false
    })
};
 
Share this answer
 
Comments
R Harshal 3-Apr-14 5:19am    
Thank you so much for your reply.
Its working.Great
Thank you so much
Harshal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900