2

I'm making three dropdowns on HTML and I make a JavaScript program to alert when there has a duplicate selected value. What should I do If after the alert the selected dropdown return its default value after the alert.

function hey() {

  var count = document.getElementsByClassName('data').length;
  //var cars = document.getElementsByClassName('data')[0].value;
  var arr = [];
  var text = "";
  var i = 0;
  while (i < count) {
    if ((document.getElementsByClassName('data')[i].value) == "") {} else {
      arr.push(document.getElementsByClassName('data')[i].value);
    }
    i++;
  }
  document.getElementById("demo").innerHTML = arr;
  var uniqueval = arr.filter(function(itm, i, arr) {
    return i == arr.indexOf(itm);
  });
  if (arr.length > uniqueval.length) {
    alert("Has duplicate")
  }
}
<div class="info">
  <select name="type" class="data" onchange="hey()">
    <option value="">---select---</option>
    <option value="teacher">Teacher</option>
    <option value="student">Student</option>
    <option value="students">Students</option>
  </select>
  <select name="class" class="data" onchange="hey()">
    <option value="">---select---</option>
    <option value="teacher">Teacher</option>
    <option value="student">Student</option>
    <option value="students">Students</option>
  </select>
  <select name="class" class="data" onchange="hey()">
    <option value="">---select---</option>
    <option value="teacher">Teacher</option>
    <option value="student">Student</option>
    <option value="students">Students</option>
  </select>
  <p id="demo"></p>
</div>
<p>When I select another dropdown and its been already selected <br> 
Theres have an alert message and when I click OK I want to return its default value which is --select--</p>

I want to return the --select-- value after the alert of the selected dropdown.

Malvz
  • 21
  • 4
  • 1
    Duplicate? why? and how? did you understand what I want to say sir? – Malvz Jan 31 '19 at 09:54
  • If you still haven't been able to solve your problem, here's a [codepen](https://codepen.io/anon/pen/bzgvLd?editors=1010) that does what you want. `Html` code is same as yours but I have simplified and removed unnecessary statements from `javascript` code. – Yousaf Jan 31 '19 at 11:41
  • Thank you so much sir. Is it not possible to reset only the value of the selected dropdown? – Malvz Jan 31 '19 at 11:50
  • Here's the [updated codepen](https://codepen.io/anon/pen/xMgzYE?editors=1010) – Yousaf Jan 31 '19 at 12:46
  • A very much THANK YOU so much sir. – Malvz Feb 01 '19 at 05:32

0 Answers0