You must Sign In to post a response.
  • Category: JavaScript

    Clear form control

    Hi,

    In my javascript clear my all form controls but i want to clear the dropdown box

    My javascript as below

    function ClearAllControls ()
    {
    for (i=0; i<document.forms[0].length; i++)
    {
    doc = document.forms[0].elements[i];
    switch (doc.type)
    {
    case "text" :
    doc.value = "";
    break;
    case "textarea" :
    doc.value = "";
    break;
    case "MultiLine":
    doc.value = "";
    break;
    case "checkbox" :
    doc.checked = false;
    break;
    case "radio" :
    doc.checked = false;
    break;

    default :
    break;
    }
    }
    }

    In this javascript how could i add to clear the dropdown box value.
    Could anybody guide to me.

    Thanks and regards
    brite
  • #763599
    Hi brite,

    Have you used empty(), method to clear the dropdown values, refer below sample code.


    $('#ddl').empty();


    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763607
    When we say 'clear form control' then it means all controls get reset. so when drop down gets reset it should be set to index 0 that is empty text selection, you should not remove all items from dropdownlist
    see below javascript snippet
    var options = document.querySelectorAll('#my_select option');
    for (var i = 0, l = options.length; i < l; i++) {
    options[i].selected = options[i].defaultSelected;
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763616
    Hi Prasad,

    I agree your answer in my case but how could i include in my javascript like

    case "dropdownlist" :
    doc.selectedindex=0;
    break;

    the above case block is wrong, but how to write correct one.

  • #763678
    Hai Brite,
    You can use the type as "select-one" for the drop-down list and then you can try:

    case "select-one":
    doc.selectedIndex = "-1";
    break;

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764066
    If you are running your application in Internet Explorer(IE) follow the steps:

    1) You should add one blank item inside dropdownlist as 1st item as follows:
    <asp:ListItem Text=""></asp:ListItem>
    2) Inside your ClearAllControls() function add following case before default case

    case "select-one":
    doc.options.length = -1;
    break;

    Hope this will help you.

    Regards,
    Shashikant Gurav (Programmer II)
    shashikantgurav22@gmail.com


Sign In to post your comments