Alex
Alex

Reputation: 55

If option selected do something

I have form. I want to do something, if the certain option is selected. I try this

if (document.getElementById('order-select').value == "Услуга") {
  alert("blabla");
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="order-select">
  <option value="Услуга" class="item-1">Услуга</option>
  <option value="Строительный проект" class="item-2">Строительный проект</option>
</select>

Problem : I want to alert every time, when option is selected. Need help

Upvotes: 2

Views: 7472

Answers (1)

Josh Crozier
Josh Crozier

Reputation: 240938

You need to attach an event listener for the change event.

When the event is fired, you can access the element through the target object on the event object.

document.getElementById('order-select').addEventListener('change', function (e) {
  if (e.target.value === "Услуга") {
    alert('Selected');
  }
});
<select id="order-select">
  <option class="item-1">--Select--</option>
  <option value="Услуга" class="item-2">Услуга</option>
  <option value="Строительный проект" class="item-3">Строительный проект</option>
</select>

Updated Example

document.getElementById('order-select').addEventListener('change', function (e) {
  if (e.target.value === "Услуга") {
    alert('Selected');
  }
});

Upvotes: 5

Related Questions