user1573724
user1573724

Reputation: 13

Generating text based on selected option in HTML using JavaScript

I am trying to create a site where you select an option from a dropdown list called contactOptions. The options has values ranging from 0 to 8. When the option is selected, I wish for text to appear underneath -- this text is contained within div tags with ID's such as #contact0 for the text corresponding to option 0. These ID's all have display: none by default. I have some JavaScript (below) -- the select tag in my html includes the line onchange = "sortForm()". Unfortunately, the script does not do anything -- the #contact0 remains invisible.

function sortForm() {
  var selection = document.getElementById("contactOptions");
  var selectedValue = selection.options[selection.selectedIndex].value;

  for (var i = 0; i <=8; i++) {
    var subobjContact = document.getElementById("contact" + i);
    if (i == selectedValue) {
      subobjContact.style.display == "block"
    } else {
      subobjContact.style.display == "none"
    }
  }
}

Upvotes: 0

Views: 696

Answers (1)

Sudip
Sudip

Reputation: 2051

Use single equal sign

subobjContact.style.display = "block"

and

subobjContact.style.display = "none" 

Upvotes: 1

Related Questions