Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to generate input fields based on value from a drop down list

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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 need to write the JSP using plain HTML and Javascript, without any sort of JQuery or other external library.

I hope anyone can help me !

Ivano C.
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you already knew this, but I thought that I should just point out that your "JSP form" with the drop-down list is actually an HTML page which was generated by some JSP.

And when you submit that form from the HTML page, it's going to go to a servlet which will take the selected value and forward to another JSP, which will include a loop to generate those three input fields which you gave as an example.

If you already knew that and had some specific question about that second JSP, then don't hesitate to post back and let us know.
 
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivano Carrara wrote:In a JSP form ...


There is no such thing as a "JSP form". It's all HTML by the time it gets to the browser, so the fact that the HTML was generated from a JSP is irrelevant.

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.


This is not something that JSP is going to help you with. This needs to be done using JavaScript in the browser.

The only thing that I think I have to use is "onChange" on the drop down list.


Yes -- that's the way you will know that the value has been changed.

I need to write the JSP using plain HTML and Javascript, without any sort of JQuery or other external library.


Why? Is this a school exercise where you are not allowed to use jQuery? If it's production code, not using jQuery (or something similar) is not advised.

I hope anyone can help me !


Write up your code and let us know what problems you are having with it. You'll get plenty of help.

I've moved this to the HTML/JavaScript forum as this is not a JSP question.
 
Ivano Carrara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bear, thank you for your reply and advices !

I'm only a Java programmer and before to come here I read some stuff about Javascript but of course I need more help from you ...

At first, I'll try to write only plain HTML and Javascript cause I have to put my final Jsp in a environment that include some Javascript/jQuery/other and I don't want to get in conflit with it.
But I'm understandig 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 and this fact was a big success for me :-)
Below I show you my 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 in the final case "Option three" or "Option four" and so on) the HTML shows two 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 travel and I need to get the ages of every Persons - excuse for my bad English !!

Thank you for some more help

Ivano C.



 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't yet answered all my questions.
 
Ivano Carrara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bear, excuse for my faul ... I still have to understand all the rules of this forum.

I'll retry...

Ok, I understood that we are talking about HTML and it is not relevant that it is generated from a Jsp page.

I agree that it is Javascript that manage the job in the browser and not Jsp.

The focus is on the onChange element of select control.

As I wrote in my previous message, I'll try to write only plain HTML and Javascript cause I have to put my final Jsp in a environment that include some Javascript/jQuery/other and I don't want to get in conflit with it or request to trhe server management to add othe jQuery or other frameworks version.

So I wrote my simple code in my previous post to looking for help and reach my goal

I hope to satisfied all the your question !

Best regards,
Ivano C.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first step is to break what you need to do into discrete steps that you can write and test. Take a stab at what that should be. (Hint: one of the steps would be something like "Establish an event hander for changes to the the select element")
 
Ivano Carrara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:The first step is to break what you need to do into discrete steps that you can write and test. Take a stab at what that should be. (Hint: one of the steps would be something like "Establish an event hander for changes to the the select element")



Dear Bear, I'm so sorry because I was looking here for some examples using HTML/Javascript to obtain my target ...

I saw you are a very expert on that but still I'm in big trouble cause I can't get one single line of code as example.

I think that my description of the case is clear and also I replied to all your questions

I continue to hope in some help for a novice as me.

Best regards,
Ivano C.
 
Ivano Carrara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Opps .. in my example of code I forgot the Input field:

 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't ask you for any code yet. I asked you for a list of steps.

If you cannot list the steps that you need to accomplish, how can you write code to achieve those steps?
 
Ivano Carrara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bear, I'm so sorry about my fault .. So I'll try again to explain what I need to obtain:

In a HTML 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 already deployed on the target server.
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.

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.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop, sit back, and take a deep breath.

You keep describing what you want to do. But that's not what you need next. What you need is the how.

Step away from the keyboard. Grab a paper and pencil. Yes, a pencil. And write the steps you need to do to accomplish this.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic