Current location: Hot Scripts Forums » General Web Coding » JavaScript » Multiple input box from combo-box


Multiple input box from combo-box

Reply
  #1 (permalink)  
Old 04-22-07, 08:08 AM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy 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...
Reply With Quote
  #2 (permalink)  
Old 04-23-07, 06:55 AM
qie's Avatar
qie qie is offline
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
Reply With Quote
  #3 (permalink)  
Old 04-23-07, 08:53 AM
Vicious's Avatar
Vicious Vicious is offline
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:
  1. function CreateInputs(selectbox){
  2.     var amountInputs = selectbox.value; // or selectbox.elements[selectbox.selectedIndex].value
  3.  
  4.     // do a loop, to create the specified amount of input boxes:
  5.     for (var i=1;i<=amountInputs;i++)
  6.     {
  7.         var textbox = document.createElement("input"); // create a new textfield
  8.         textbox.setAttribute("name","in" + i); // give a unique name
  9.         textbox.setAttribute("type","text"); // make sure it is of the type "text"
  10.  
  11.         // now we need to append the new textfield to the document:
  12.         selectbox.parentNode.appendChild(textbox);
  13.     }
  14. }

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
Reply With Quote
  #4 (permalink)  
Old 04-23-07, 12:49 PM
qie's Avatar
qie qie is offline
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.
Reply With Quote
  #5 (permalink)  
Old 04-23-07, 01:26 PM
Vicious's Avatar
Vicious Vicious is offline
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:
  1. function CreateInputs(selectbox){
  2.     // clean previous input boxes:
  3.     var f = selectbox.form; // get the parent form
  4.     var inputs = f.getElementsByTagName("input");
  5.     for (var j=0;j<inputs.length;j++)
  6.     {
  7.         if (inputs[j].getAttribute("name").indexOf("in") == 0)
  8.         {
  9.             // remove that element
  10.             var p = inputs[j].parentNode;
  11.             p.removeChild(inputs[j]);
  12.         }
  13.     }
  14.  
  15.     var amountInputs = selectbox.value; // or selectbox.elements[selectbox.selectedIndex].value
  16.  
  17.     // do a loop, to create the specified amount of input boxes:
  18.     for (var i=1;i<=amountInputs;i++)
  19.     {
  20.         var textbox = document.createElement("input"); // create a new textfield
  21.         textbox.setAttribute("name","in" + i); // give a unique name
  22.         textbox.setAttribute("type","text"); // make sure it is of the type "text"
  23.  
  24.         // now we need to append the new textfield to the document:
  25.         selectbox.parentNode.appendChild(textbox);
  26.     }
  27. }

I hope it works...
__________________
Jack Bauer makes Chuck Norris cry
Reply With Quote
  #6 (permalink)  
Old 04-28-07, 03:38 PM
qie's Avatar
qie qie is offline
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
Reply With Quote
  #7 (permalink)  
Old 04-29-07, 05:55 PM
jfulton's Avatar
jfulton jfulton is offline
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.
Code:
<script>
function CreateInputs(selectbox){
    var amountInputs = selectbox.value; // or selectbox.elements[selectbox.selectedIndex].value
    var txtHTML = "";
    // do a loop, to create the specified amount of input boxes:
    for (var i=1;i<=amountInputs;i++)
    {
        txtHTML += "<input type=\"text\" name=\"in" + i + "\" />";
    }
    document.getElementById("textBoxes").innerHTML = txtHTML;
}
</script>
...
<div id="textBoxes"></div>
...
Reply With Quote
  #8 (permalink)  
Old 05-04-07, 07:39 AM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Wink

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
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Query Problem with Multiple select box elessar ASP 12 01-27-07 04:57 PM
Combo box image change with names from database Frogger ASP 4 07-09-06 04:54 AM
Combo Box Question iamtat Visual Basic 3 10-18-05 01:49 PM
Forms help input box switch to Select box dstran JavaScript 1 04-19-05 12:17 AM
help in combo box mathfxr ASP 0 12-28-04 10:05 PM


All times are GMT -5. The time now is 11:26 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.