How to pass parameters on onChange of HTML select with JavaScript?

Spread the love

Sometimes, we want to pass parameters on onChange of HTML select with JavaScript.

In this article, we’ll look at how to pass parameters on onChange of HTML select with JavaScript.

How to pass parameters on onChange of HTML select with JavaScript?

To pass parameters on onChange of HTML select with JavaScript, we can use the event object.

For instance, we write

<select id="comboA">
  <option value="">Select combo</option>
  <option value="Value1">Text1</option>
  <option value="Value2">Text2</option>
  <option value="Value3">Text3</option>
</select>

to add the select element.

Then we write

const select = document.querySelector("select");
select.onclick = (e) => {
  const value = e.target.value;
  console.log(value);
};

to select the select element with querySelector.

Then we set its onclick property to a function that gets the value attribute ofi the selected option with e.target.value.

Conclusion

To pass parameters on onChange of HTML select with JavaScript, we can use the event object.

Leave a Reply

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