Chrome form reset don't change display select element

Viewed 412

When I reset the form the select element value reset but still display the old value

I'm using this to reset the form

$('#testForm')[0].reset();

In this example the 'Two' option is selected by default. If I change the selection and press 'reset form' the select value change back but still shows the old value

in Edge its working

https://jsfiddle.net/z6vk91sL/

Google Chrome 83.0.4103.61 (Official Build) (64-bit) (cohort: Stable)

$('#testFormReset').on("click", () => {
  $('#testForm')[0].reset();
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id='testForm'>
  <select>
    <option value='1'>One</option>
    <option selected value='2'>Two</option>
    <option value='3'>Three</option>
  </select>

  <button id='testFormReset' type='reset'>
    reset form
  </button>
</form>

2 Answers

Looks like you are having this issue in the Google Chrome and MS Edge browser as both browsers use the same browser engine.

As the issue is already reported and fixed on the Chromium site.

Here I am sharing you the workaround that you can use temporarily.

<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


</head>
<body>
<form id="testForm">
  <select id="my_select">
    <option value='1'>One</option>
    <option value='2' selected>Two</option>
    <option value='3'>Three</option>
  </select>

  <button id='testFormReset'>
    reset form
  </button>
</form>
<script>
$('#testFormReset').on("click", () => {
    $('#my_select option').prop('selected', function() {
        return this.defaultSelected;
    });

    $('#testForm')[0].reset();
 
})
</script>
</body>
</html>

Output in the MS Edge(on the left side) and Google Chrome browser:

enter image description here

Related