0

I'm trying to replace a drop down menu I have with a text field if someone chooses the "other" option. Right now in my code I have it replacing it with a paragraph element because I'm too lazy to find the exact constructor arguments for setting up a text field. However, when "other" is selected nothing happens.

Does anyone know what's wrong with this?

<html>
<head>
<script type="text/javascript">
function testfunc(arg) {
    if(arg.value == "other") {
        document.thing.replaceChild(document.test, document.thing.selection)
    }
    else {
        alert("stuff")
    }
}
</script>
<body>
<form name="thing">
<select name="selection" onchange="testfunc(document.thing.selection.options[document.thing.selection.selectedIndex])">
<option>yes</option>
<option>no</option>
<option>other</option>
</select>
</form>
<p name="test">lkjsdf</p>
</body>
</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SummerCodin
  • 263
  • 1
  • 6
  • 10

3 Answers3

2

the function should be like this! Other not other ! its case sensitive (on linux anyway...not sure about other os) Otherwise thanks...been looking for this...cool way of show/hide

<script type="text/javascript">
function show_txt(arg,arg1)
{
if(document.getElementById(arg).value=='Other')
{
document.getElementById(arg1).style.display="block";
document.getElementById(arg).style.display="none";
}
else
{
document.getElementById(arg).style.display="block";
document.getElementById(arg1).style.display="none";
}
}
</script>
musefan
  • 47,875
  • 21
  • 135
  • 185
Stan
  • 21
  • 2
0
function show_txt(arg,arg1)
{
if(document.getElementById(arg).value=='other')
{
document.getElementById(arg1).style.display="block";
document.getElementById(arg).style.display="none";
}
else
{
document.getElementById(arg).style.display="block";
document.getElementById(arg1).style.display="none";
}
}

The HTML code here :

<select id="arg" onChange="show_txt('arg','arg1');">
<option>yes</option>
<option>No</option>
<option>Other</option>
</select>
<input type="text" id="arg1" style="display:none;">
Gitesh Dang
  • 182
  • 1
  • 14
0

Hey, I saw your code, you need some practice,

You forgot to close tagh. second always use ids instead of names. Third a form cannot access <p> tagh. fourth you can not access elements other then form, using form prefixes. 5th either you can create <p> tagh dynamicaly or palce it on page with display none. Check my code. Also this what you can do to dynamically genrate it.

var newP = document.createElement("p"); var txt = 'lkjsdf'; var newT = document.createTextNode(txt); newP.appendChild(newT);

<html>
<head></head>
<script type="text/javascript">
function testfunc(arg) {
    if(arg.value == "other") {
        document.thing.removeChild(document.thing.selection);
        document.getElementById('testText').style.display='inline';
    }
    else {
        alert("stuff");
    }
}
</script>
<body>
<form name="thing">
<select name="selection" onchange="testfunc(document.thing.selection.options[document.thing.selection.selectedIndex])">
<option>yes</option>
<option>no</option>
<option>other</option>
</select>
<p id="testText" style="display:none">lkjsdf</p>
</form>
</body>
</html>
Waqar Alamgir
  • 9,828
  • 4
  • 30
  • 36