Question

<script type="text/javascript">
    function myfunction() {
        if (document.getElementById('myform').value == "yes") {
            document.getElementById('yesinput').style.display = '';
        } else {
            document.getElementById('yesinput').style.display = 'none';
        }
    }
</script>

<select name="myform" id="myform" onchange="myfunction()">
<option selected="selected">please select</option>
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>

<div id="yesinput" style="display:none">
<input name="input" type="text" />
</div>

<div id="noinput" style="display:none">
<input name="input" type="text" />
</div>

Help ya. How to make if we select No will have another input field below the id="noinput".

Now it's working if we select Yes. let me know

Was it helpful?

Solution

function myfunction() {
    if (document.getElementById('myform').selectedIndex == 0) {
        document.getElementById('noinput').style.display = 'none';
        document.getElementById('yesinput').style.display = 'inline';
    } else {
        document.getElementById('noinput').style.display = 'inline';
        document.getElementById('yesinput').style.display = 'none';
    }
}

OTHER TIPS

You can construct a new Element and append it to the DOM via Javascript. An example:

var newInput = document.createElement("input");
newInput.setAttribute("id", "newElement");
newInput.setAttribute("type", "text");
document.body.appendChild(newInput);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top