wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes java/html how to add fields  when we click button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "java/html how to add fields  when we click button" Watch "java/html how to add fields  when we click button" New topic
Author

java/html how to add fields when we click button

giri shetty
Greenhorn

Joined: Jan 22, 2004
Posts: 27
Hi guys,
can you please explain me how to add textfields in a jsp page when i click a button .if i click a button three times three buttons should be added to that jsp page.
thanks in advance
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I think you need to tell us a bit more about what you need. Presumably these buttons are supposed to do something when they are clicked? Do they need different names? different text?
There are two basic approaches to this sort of problem:
1. The round-trip approach: when the button is clicked, the form is submitted to itself. The JSP contains a bean which counts the number of times the "add" button has been clicked, and generates that many button declarations to the output stream when the page is rendered.
2. The JavaScript approach: when the button is clicked, it runs sone JavaScript which modifies the document object model to add another button to the form.
It's not at all clear which of these might be appropriate to your situation.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
giri shetty
Greenhorn

Joined: Jan 22, 2004
Posts: 27
I need the second Approach
giri shetty
Greenhorn

Joined: Jan 22, 2004
Posts: 27
Hi Frank,
i need the second approach , when i click a button every time one text field should be added to that jsp page.
thanks
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If you want to do this in JavaScript, then it's not really got anything to do with JSP. With this in mind I have moved this thraed to our HTML and JavaScript forum. Please continue the discussion there.
himanshu patel
Ranch Hand

Joined: Feb 03, 2003
Posts: 205
Originally posted by giri shetty:
Hi guys,
can you please explain me how to add textfields in a jsp page when i click a button .if i click a button three times three buttons should be added to that jsp page.
thanks in advance

Do you want to add textfields or button.
There are two ways to add element dynemically.
(1) using document.write() property
(2) using document.createElement() property
Here is an example using (2) property.
------------------------------------
<html><head></head><body>
<form action=inputox.html name="form1">
<table>
<tbody id="tbody1">
<tr>
<td>
This is Row 1
<input type="button" value="Click to Add a Row" name="button1" >
</td>
</tr>
<tr id="lastTR">
<td id="lastTD">
This is Row 2
</td>
</tr>
</tbody>
</table>
<input type=submit>
</form>
<script>
textCount = 1;
function AddRow()
{
var tbody1 = document.getElementById("tbody1");
var lastTR = document.getElementById('lastTR');
var newTR = document.createElement('TR');
var newTD = document.createElement('TD');

tbody1.insertBefore(newTR, lastTR);
newTR.appendChild(newTD);
newTD.innerHTML="<input type=text name=textField" + textCount +">";
textCount++;
}
</script>
</body></html>
--------------------------------------
Please use on click proprty to call AddRow() function at first button ie. "button1". I was not able to post if i insert this property at button1.
Hope this will helpful to you.
[ January 26, 2004: Message edited by: himanshu patel ]
[ January 26, 2004: Message edited by: himanshu patel ]

If you want to become a rich, do not work for others but make others to work for you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java/html how to add fields when we click button