/    Sign up×
Community /Pin to ProfileBookmark

How to generate a quantity of Input fields based on value from a drop down list

I’m not expert on HTML/Javascript so I hope someone can help me.

In a JSP form I need to acquire the age of some persons.
I don’t know how many persons there are in advance.

So I need to dinamically generate a quantity of input fields where to put the age of persons based on the value of persons selected from a drop down list.

The quantity of persons is determinated by the value selectd from the drop down list.

For example, if the user select the value “3” from the drop down list, in the JSP form I need to dinamically generate 3 input fields to acquire the age of the 3 persons.
If the user select the value “0” from the drop down list, in the JSP form I don’t need to generate input fields.
The only thing that I think I have to use is “onChange” on the drop down list.

I’ll try to write only plain HTML and Javascript cause I don’t want to get in conflit with jQuery or other libraries.
I’m thinking that using only HTML/Javascript I can get good results too.

At the same time I need to obtain my target without to reload the HTML page.

In my previous days I learned to hide/show an HTML input field – below I show you my code:

[CODE]<select name=”opt” label=”some opts” id=”opts” onchange=”showdv(this,’one’,’two’);” >
<option value=”one” selected=”true”>Option one</option>
<option value=”two” >Option two</option>
</select>[/CODE]

[CODE]<div id=”codFiscaleField” style=”display:none;”>
<input name=”codFiscale” size=”50″></input>
</div>[/CODE]

[CODE]<script>
function showdv(obj,id1,id2) {
txt=obj.options[obj.selectedIndex].text;
document.getElementById(“codFiscaleField”).style.display=’none’;

if (txt.match(id1)) {
document.getElementById(“codFiscaleField”).style.display=’block’;
document.getElementById(“codFiscaleFieldXxx”).value=txt
}

if (txt.match(id2)) {
document.getElementById(“codFiscaleField”).style.display=’block’;
document.getElementById(“codFiscaleFieldXxx”).value=txt
}
}
</script>[/CODE]

In fact when I pick the “Option two” the script shows the input field.

But my final target is different: I need that when the user select the “Option two” (or “Option three” or “Option four” and so on) the HTML shows two Input fields (or three or four Input fields).

The option selected determine the number of Input fields, for example:

User selected “Option three” then the HTML shows three Input fields.
User selected “Option four” then the HTML shows four Input fields.

My final job is: The select determine the number of Persons in the list and I need to get the ages of every Persons.

Thank you for some more help

Ivano C.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumApr 12.2016 — Try this:
[CODE] <select name="opt" id="opts" onchange="showdv(this);">
<option value="1" selected="true">Option one</option>
<option value="2">Option two</option>
<option value="3">Option three</option>
</select>
<form id="codFiscaleField">
</form>

<script>
function showdv(obj) {
frm = document.getElementById("codFiscaleField");
frm.innerHTML = "";
nr = parseInt(obj.options[obj.selectedIndex].value);
for (var i = 0; i < nr; i++) {
inp = document.createElement("input");
inp.setAttribute("name", "codFiscale" + i);
frm.appendChild(inp);
}
}
</script>[/CODE]

You can add labels to the fields if required.
Copy linkTweet thisAlerts:
@icarraraauthorApr 12.2016 — Sempervivum !! Thank you ! You are great ! ?

It worked very good ... How I can vote you or give visibility to your reply in this forum ?

And yes, I need to add labels to the fields - please could you help me more ?

Thank you in advance,

Ivano C.
Copy linkTweet thisAlerts:
@SempervivumApr 12.2016 — And yes, I need to add labels to the fields[/QUOTE]To add these one would have to know the text of the labels. Is it freely defined or just a fixed text including a number?
[CODE] for (var i = 0; i < nr; i++) {
var inp = document.createElement("input");
inp.setAttribute("name", "codFiscale" + i);
var lbl = document.createElement("label");
lbl.innerHTML = "Label Nr. " + (i + 1);
frm.appendChild(inp);
frm.appendChild(lbl);
}[/CODE]
Copy linkTweet thisAlerts:
@icarraraauthorApr 12.2016 — Yes ! It worked !! Thank you Sempervivum !

I personalized the labels - actually my code is the below:

[CODE]
<aui:script>
function showBabyAge(obj) {
frm = document.getElementById("babyAgeField");
frm.innerHTML = "";
nr = parseInt(obj.options[obj.selectedIndex].value);
for (var i = 0; i < nr; i++) {
var inp = document.createElement("input");
inp.setAttribute("name", "babyAge" + i);
inp.setAttribute("size", "5");
var lbl = document.createElement("label");
lbl.innerHTML = '<%=LanguageUtil.get(pageContext, "ob-babyAge1") + StringPool.SPACE%>' + (i + 1);
frm.appendChild(lbl);
frm.appendChild(inp);
}
}
</aui:script>
[/CODE]


The last step is to force the user to put some value in the Input fields - maibe it is sufficient to render the background of the field in red color or eventually show an alert message ...

The fields are inside a form and I know that I need to return false to this form ...

Please could you suggest me this last steps ?

Best regards,

Ivano C.
Copy linkTweet thisAlerts:
@SempervivumApr 12.2016 — This should do the job:
[CODE] <form id="codFiscaleField">
<button id="mybutton" type="submit" value="Submit">Submit</button>
</form>

<script>
frm = document.getElementById("codFiscaleField");
frm.addEventListener("submit", function (ev) {
ev.preventDefault();
var inp = document.getElementById("codFiscaleField").getElementsByTagName("input");
for (var i = 0; i < inp.length; i++) {
if (inp[i].value == "") inp[i].style.backgroundColor = "red";
else inp[i].style.backgroundColor = "white";
}
return false;
});
function showdv(obj) {
while (frm.firstChild) {
frm.removeChild(frm.firstChild);
}
nr = parseInt(obj.options[obj.selectedIndex].value);
var newln = document.createElement("br");
var subm = document.createElement("button");
subm.innerHTML = "Submit";
frm.appendChild(subm);
frm.appendChild(newln);
for (var i = 0; i < nr; i++) {
var inp = document.createElement("input");
inp.setAttribute("name", "codFiscale" + i);
var lbl = document.createElement("label");
lbl.innerHTML = "Label Nr. " + (i + 1);
frm.appendChild(inp);
frm.appendChild(lbl);
frm.appendChild(newln);
}
}
showdv(document.getElementById("opts"));

</script>[/CODE]
Copy linkTweet thisAlerts:
@icarraraauthorApr 13.2016 — Hi Sempervivum, thank you for all your suggests.

I need to enter in touch with you for some more requests that are not useful for this forum.

Please could you contact me via email ?

My address is icarrara AT studio5 DOT it

Thank you in advance !

Ivano C.
×

Success!

Help @icarrara spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...