Categories
JavaScript Answers

How to fire a change event on a HTML select element if the new value is the same as the old with JavaScript?

Spread the love

Sometimes, we want to fire a change event on a HTML select element if the new value is the same as the old with JavaScript.

In this article, we’ll look at how to fire a change event on a HTML select element if the new value is the same as the old with JavaScript.

How to fire a change event on a HTML select element if the new value is the same as the old with JavaScript?

To fire a change event on a HTML select element if the new value is the same as the old with JavaScript, we add an empty option.

For instance, we write

<select onchange="jsFunction()">
  <option></option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

to add an empty option element so that jsFunction is called whether the selected value is the same as the old one or not.

Conclusion

To fire a change event on a HTML select element if the new value is the same as the old with JavaScript, we add an empty option.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *