Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Display div if two specific select option value is selected?

I am trying to display a div/table if user select two specific option value from two select drop down list.
Example:

The first select drop down list consist of options called, Monday and tuesday.
The second select drop down list consist of options called, january and febuary.

If user select an option like "Monday" and "January", i want to show something like "Option chosing is monday and january" same thing like tuesday and january, and eccc, but for now i’m only able to print test for only one of the selection.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my code:

function optionClicked() {

  let userPicked = document.getElementById("days").value;
  var div = document.getElementById("div");
  if (userPicked == '1') {
    div.innerHTML = "You clicked option 1";
  } else if (userPicked == '2') {
    div.innerHTML = "You clicked option 2.";
  } else {
    alert("No choice made.");
  }
}
<select id="days">
  <option value="1">monday</option>
  <option value="2">tuesday</option>
</select>

<select id="months">
  <option value="J">january</option>
  <option value="F">febuary</option>
</select>

<input type=button value="option chosen" onclick="optionClicked()" />
<p>

</p>

>Solution :

Here’s how to get text of the selectedIndex in a <select>

function optionClicked() {
  let days = getSelectedText(document.getElementById("days"))
  let months = getSelectedText(document.getElementById("months"))
  var div = document.getElementById("div");
  div.innerHTML = `You clicked option ${days} and ${months}`;
}

function getSelectedText(select) {
  return select.options[select.selectedIndex].text
}
<select id="days">
  <option value="1">monday</option>
  <option value="2">tuesday</option>
</select>

<select id="months">
  <option value="J">january</option>
  <option value="F">febuary</option>
</select>

<input type=button value="option chosen" onclick="optionClicked()" />
<p id="div">
</p>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading