1

Hello i want to refresh this select without refreshing all page

<select name="number" id="number">
             <option value="1">first</option>
             <option value="2">second</option>
             <option value="3">third</option> 
</select>

I had reset it because I needed it to perform a few tasks but to update it as in the beginning, I have to update all my page but I just want to refresh the combobox itself without having to refresh the page. What code should I put ?

Dev1996
  • 81
  • 1
  • 2
  • 12
  • Have a look here: https://stackoverflow.com/questions/7373058/changing-the-selected-option-of-an-html-select-element – admcfajn Jun 18 '17 at 22:33
  • @admcfajn I do not need to change the elements but I just want to reset the select as it was. There is no way to update a select? – Dev1996 Jun 18 '17 at 22:38
  • I checked the link but it is not the same problem – Dev1996 Jun 18 '17 at 22:39
  • That's 100% how you do it. You'd use javascript to select the element and then change it's value. Now, I may have misunderstood you... Adding `` would keep that one selected after refresh, in case a different selection had been made & was being stored by the browser. – admcfajn Jun 18 '17 at 22:40
  • You did not understand me! The form is empty when I click on the empty button and after clicking on the add button it remains empty until I have refreshed the page but I want that when I click on the add button it loads As default – Dev1996 Jun 18 '17 at 22:48
  • I'm sorry, I can't solve that problem by only seeing the html. I'd need to see the code relevant to the add-button to help you find a solution. – admcfajn Jun 18 '17 at 22:50
  • *«when I click on the add button it loads As default»* -- So show the HTML for the «add» button. Show the HTML for the «empty» button aswell. If that is more simple, show HTML for the whole form. **And show the JS scripts** you actually have. – Louys Patrice Bessette Jun 18 '17 at 22:57

2 Answers2

2

If you want to reset the selection, please try this.

$('#number').val('');

Or

$('select option:selected').removeAttr('selected');

If you want to remove all the option from the dropdown and make it empty, try this.

$('#number').empty();
Ayaz
  • 2,111
  • 4
  • 13
  • 16
1

If you have more than one 'select' you can always create a function and use this:

const selects = this.elementRef.nativeElement.querySelectorAll('select'); 
selects.forEach(select => {
   select.value = '';
});
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83