0

once I submit a form I want to reset it. Everything is resetting except the select menu. I have tried this:

 const catDrop = document.getElementById('category');
 catDrop.selectedIndex = 0;

In the code, it looks like a normal option:

          <form id="add-listing">
            <div class="col-lg-12">
                <div id="add-listing">
                    <div class="add-listing-section">
                        <div class="add-listing-headline">
                            <h3>Basic Information</h3>
                        </div>
                        <div class="row with-forms">
                            <div class="col-md-12">
                                <h5>Item Name <i class="tip" data-tip-content=""></i></h5>
                                <input class="search-field" type="text" name="title" id="title" />
                            </div>
                        </div>
                        <div class="row with-forms">
                            <div class="col-md-12">
                                <h5>Category</h5>
                                <select class="chosen-select-no-single" name="category" id="category">
                                    <option value="0">Select Category</option>
                                    <% for (const categories of cats) { %>
                                    <option value="<%= categories.catName %>">
                                        <%= categories.catName %>
                                    </option>
                                    <% } %>
                                </select>
                            </div>
                        </div>
                        <div class="row with-forms">
                                <input type="file" name="files" id="image" data-fileuploader-limit="3" data-fileuploader-maxSize="5" data-fileuploader-extensions="jpg, png, jpeg">
                        </div>
                        <div class="row with-forms">
                            <div class="col-md-12">
                                <h5>Description</h5>
                                <textarea class="WYSIWYG" name="description" cols="40" rows="3" id="description"
                                    spellcheck="true"></textarea>
                            </div>
                        </div>
                    </div>
                    <input type="hidden" name="_csrf" value="<%= csrfToken %>" id="csrf">
                    <button type="submit" class="button preview addItem">Add Item <i class="fa fa-arrow-circle-right"></i></button>
                    <div class="error"></div>
                    <div class="successmsg"></div>
                </div>
            </div>
        </form>

But it does not reset the select menu. When taking a look in console what it looks like, it seems that it is styled as an unordered list with list items like this:

<select class="chosen-select-no-single" name="category" id="category" style="display: none;">
 <option value="0">Select Category</option>
 <option value="Stuff">
  Stuff
</option>                                        
</select>

 <div
  class="chosen-container chosen-container-single chosen-container-single-nosearch chosen-container-active"
  style="width: 100%;"
  title=""
  id="category_chosen"
>
  <div class="chosen-drop">
    <div class="chosen-search">
      <input type="text" autocomplete="off" readonly="" />
    </div>
    <ul class="chosen-results">
      <li class="active-result" data-option-array-index="0" style="">
        Select Category
      </li>
      <li class="active-result" data-option-array-index="11" style="">
        Stuff
      </li>
    </ul>
  </div>
</div>

This is the item I want to set it back to once the form has submitted.

user8463989
  • 2,275
  • 4
  • 20
  • 48
  • please share the complete html – brk Jan 22 '19 at 08:18
  • From my code editor or from console? – user8463989 Jan 22 '19 at 08:19
  • you are probably using a `plugin` please add that `plugin` in the tags. So, the right people can help.. – vikscool Jan 22 '19 at 08:19
  • @vikscool, I have posted some more code. I hope that helps – user8463989 Jan 22 '19 at 08:25
  • @brk, I posted all the output in console. Does that help? – user8463989 Jan 22 '19 at 08:25
  • i do not see any form – brk Jan 22 '19 at 08:26
  • @brk, I added my whole form now – user8463989 Jan 22 '19 at 08:28
  • you are probably using `jQuery-chosen` library have a look at [Chosen select an option by default with jQuery](https://stackoverflow.com/a/9161115/2417602) – vikscool Jan 22 '19 at 08:29
  • @user8463989 and where is the function for submit? Is it through ajax? or where are you writing the code to reset the select – brk Jan 22 '19 at 08:30
  • try changing `` like this, might work. – Kuru Jan 22 '19 at 08:30
  • 1
    Looks like this uses https://harvesthq.github.io/chosen/ ? Then you need to call `.trigger("chosen:updated");`after setting the select field value. https://harvesthq.github.io/chosen/#change-update-events – misorude Jan 22 '19 at 08:30
  • do you know what plugin are you using for this select field? as you can see, you `` probably resets but it doesn't trigger a change to your plugin to change the content of `
    ` if you know what triggers your plugin, just do that manually for first value in `
    – fila90 Jan 22 '19 at 08:30
  • I honestly don't know if it uses a plugin and what it is. I will try dig around to find out... – user8463989 Jan 22 '19 at 08:30
  • @brk, I am using axios to submit the form and in my then() block I am showing the success message and trying to reset the form. All the other fields reset correctly. `.then(response => { document.getElementById('add-listing').reset(); const catDrop = document.getElementById('category'); catDrop.selectedIndex = 0;` – user8463989 Jan 22 '19 at 08:32
  • 1
    try adding this `$("#category").trigger("chosen:updated");` after `catDrop.selectedIndex = 0;` – fila90 Jan 22 '19 at 08:34
  • I think one of these comments is correct here as it looks like the plugin is called CHOSEN – user8463989 Jan 22 '19 at 08:35
  • @fila90, is there a javascript alternative? I don't want to mix my vanilla javascript with jQuery – user8463989 Jan 22 '19 at 08:36
  • @fila90, that code worked 100% – user8463989 Jan 22 '19 at 08:38
  • @misorude, sorry, I see you also posted this. So many comments all at once, I missed some! – user8463989 Jan 22 '19 at 08:39
  • 1
    no idea how plugin works but it probably uses custom events, you can try a fire `chosen:update` event with this https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent but not sure will it work. good luck `catDrop.dispatchEvent("chosen:updated");` at the end of the day, you're using jQuery plugin, so don't worry about it this point :D – fila90 Jan 22 '19 at 08:45