Multiple input box from combo-box

04-22-07, 08:08 AM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Multiple input box from combo-box

Dear friends...,
How to create multiple input text from combo-box options?
e.g:
I have 1 combobox with 3 options.
the values is 0,1,2,3
when i select option 1, will be show 1 input text box.
when i select option 2, will be show 2 input text box.
...
but the default is 0 (zero), and no input text box will show.
please help me...
|

04-23-07, 06:55 AM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi all,
i have one <select> form with options (0,1,2,3), when the value is change, when i select option 2, will be show 2 input text.
eg:
NORMAL
(when 0 selected is nothing happend)
-------------------------------
<select name=test>
<option value=0 selected>0</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
WHEN I SELECT ONE OPTION
(when 2 selected, will be create/show new input text)
-------------------------------
<select name=test>
<option value=0>0</option>
<option value=1>1</option>
<option value=2 selected>2</option>
<option value=3>3</option>
</select>
<input type=text name=in1 /> <-- new when 2 opt is selected
<input type=text name=in2 /> <-- new when 2 opt is selected
help me please...
thanks
|

04-23-07, 08:53 AM
|
 |
Community VIP
|
|
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
adjust the code for your select box, so it executes a Javascript function when you change the value of that selectbox:
html Code:
<select name="test" onchange="CreateInputs(this)">
Then you create a Javascript function "CreateInputs", with the selectbox as input:
javascript Code:
function CreateInputs(selectbox){ var amountInputs = selectbox.value; // or selectbox.elements[selectbox.selectedIndex].value // do a loop, to create the specified amount of input boxes: for (var i=1;i<=amountInputs;i++) { var textbox = document.createElement("input"); // create a new textfield textbox.setAttribute("name","in" + i); // give a unique name textbox.setAttribute("type","text"); // make sure it is of the type "text" // now we need to append the new textfield to the document: selectbox.parentNode.appendChild(textbox); } }
This code is quite simple, and you should add some custom code to delete surplus textboxes. (e.g. you first select 3 => 3 textboxes are created. When you then select 2, there will be 2 new textboxes, totalling to 5 textboxes) So don't forget to add some code to remove all existing textboxes first, and then create the new ones.
__________________
Jack Bauer makes Chuck Norris cry
|

04-23-07, 12:49 PM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Vicious,
that script has successfully create input text, but why always gained when i select the other options?
That i want is, when i select 1, will create/show one input text, and when i select 2, will create/show two input text ( not equal to three). and when i select 0 (default) there is no input text will create/show.
|

04-23-07, 01:26 PM
|
 |
Community VIP
|
|
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, that was explained at the bottom of my code actually. But since you're asking so nicely, I'll give it for free:
javascript Code:
function CreateInputs(selectbox){ // clean previous input boxes: var f = selectbox.form; // get the parent form var inputs = f.getElementsByTagName("input"); for (var j=0;j<inputs.length;j++) { if (inputs[j].getAttribute("name").indexOf("in") == 0) { // remove that element var p = inputs[j].parentNode; p.removeChild(inputs[j]); } } var amountInputs = selectbox.value; // or selectbox.elements[selectbox.selectedIndex].value // do a loop, to create the specified amount of input boxes: for (var i=1;i<=amountInputs;i++) { var textbox = document.createElement("input"); // create a new textfield textbox.setAttribute("name","in" + i); // give a unique name textbox.setAttribute("type","text"); // make sure it is of the type "text" // now we need to append the new textfield to the document: selectbox.parentNode.appendChild(textbox); } }
I hope it works...
__________________
Jack Bauer makes Chuck Norris cry
|

04-28-07, 03:38 PM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi,
the code not work fine.
you can test it at the link below:
http://meta.uk.to/test/select.html
|

04-29-07, 05:55 PM
|
 |
Community VIP
|
|
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What if you store the inputs in a div. Then on each "select" change, you can use the innerHTML property to add the text fields. That way you don't need to worry about adding or removing the correct number each time.
|

05-04-07, 07:39 AM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yes yes yes... it's works ! 
thanks jfulton & Vicious
to all, you can try at: http://meta.uk.to/test/select.html
maybe you need it
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|