I want my form to extend in different ways depending on the selection of a drop down box.
I have got my JavaScript working so that onchanging my selection it will carry out something. But I am trying to use the appendTo()
method as I have used before; for adding a single input, to work for implementing a div and a few inputs. Its not working, hence the post 😉
So I was hoping you could enlighten me as to a better method?
Here is my current code (courtesy of Mark):
JavaScript
x
16
16
1
var selectmenu = document.getElementById("type");
2
selectmenu.onchange = function() {
3
var chosenoption = parseInt(this.options[this.selectedIndex].value);
4
switch (chosenoption) {
5
case 1:
6
$('<input type="text" value="1"/>').appendTo('#temp');
7
break;
8
case 2:
9
$('<input type="text" value="2"/>').appendTo('#temp');
10
break;
11
case 3:
12
$('<input type="text" value="3"/>').appendTo('#temp');
13
break;
14
}
15
};
16
note: updated since answers.
here is constructed in jsfiddle
It does nothing currently.
Please help? 😀
Advertisement
Answer
I think value you are trying to case on is a string not a number
try this
JavaScript
1
20
20
1
$(document).ready(function(){
2
$('#type').change(function() {
3
if( $(this).val() != "nothing" ){
4
switch ($(this).val()) {
5
case "1":
6
$('<input type="text" value="item 1" />').appendTo('#temp');
7
break;
8
case "2":
9
$('<input type="text" value="item 2" />').appendTo('#temp');
10
break;
11
case "3":
12
$('<input type="text" value="item 3" />').appendTo('#temp');
13
break;
14
}
15
}else{
16
alert($(this).val());
17
}
18
});
19
})
20