GeeCON Prague 2014*
The moose likes HTML, CSS and JavaScript and the fly likes How to populate a value in text box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to populate a value in text box" Watch "How to populate a value in text box" New topic
Author

How to populate a value in text box

Baji Roy
Greenhorn

Joined: Jul 21, 2009
Posts: 5
Hi All,
Thorugh jsp, a page displayed with some fields like radio button, textbox (names A), textbox1 ( named B)etc. Here textbox A have some value. This is done through one jsp(say X)when I click the radio button, the textbox A value is selected and some logic will be performed and in same page again one other form will be displayed.
The second form contains again a radio button, textbox value etc. This is something like anotehr jsp(say Y)

My requirement is when I click the radio button in second form data of same page, the corrosponding textbox value shoudl go to the first form textbox1 (named B) place.
Now both X and Y are in same page.This has to be done using javascript only

How to do this in javascript.Please do the need ful.
binayakumar patel
Greenhorn

Joined: Jun 26, 2009
Posts: 27
Are you sure there is multiple jsp is getting dynamically include to your one jsp file.
please check the code may be there is multiple <div> will be there . where while click on checkbox
it will make the <div> visiable...

If this is the case , if you are using multiple <div> and making it enable by clicking on check box then you can do it by javascript.


If this is not the scenario and you are calling the multiple jsp by <jsp:include> on click on checkbox... then javascript will not
help. You have to take a bean class and has to set the value in the jspbean and populate that bean value to the first jsp..

as when it will call first time there is no value in the bean,... when you click the second jsp you have to set the text value to the jspbean and has to call the same page....

while doing this ... the textfield in the first page will get the value from the jspbean .

Please note that keep the scope of the jspbean as session.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Do you know how to set a textbox value with JavaScript? Have you worked with the onchange event before?

Eric
Baji Roy
Greenhorn

Joined: Jul 21, 2009
Posts: 5
binayakumar patel wrote:Are you sure there is multiple jsp is getting dynamically include to your one jsp file.
please check the code may be there is multiple <div> will be there . where while click on checkbox
it will make the <div> visiable...

If this is the case , if you are using multiple <div> and making it enable by clicking on check box then you can do it by javascript.


If this is not the scenario and you are calling the multiple jsp by <jsp:include> on click on checkbox... then javascript will not
help. You have to take a bean class and has to set the value in the jspbean and populate that bean value to the first jsp..

as when it will call first time there is no value in the bean,... when you click the second jsp you have to set the text value to the jspbean and has to call the same page....

while doing this ... the textfield in the first page will get the value from the jspbean .

Please note that keep the scope of the jspbean as session.


Thank you for your inputs. Yes there is <div> in the forms. So it must be done through javascript.
Please do the needful.
Baji Roy
Greenhorn

Joined: Jul 21, 2009
Posts: 5
Eric Pascarello wrote:Do you know how to set a textbox value with JavaScript? Have you worked with the onchange event before?

Eric


Yes Eric. I am not extremely worked with JS.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
so add an onchange event



It would be better to do it in an unobtrusive manner, but that is the basic idea.

Eric
Baji Roy
Greenhorn

Joined: Jul 21, 2009
Posts: 5
Eric Pascarello wrote:so add an onchange event



It would be better to do it in an unobtrusive manner, but that is the basic idea.

Eric


Thank you. Let me try and update you.
Baji Roy
Greenhorn

Joined: Jul 21, 2009
Posts: 5
Baji Roy wrote:
binayakumar patel wrote:Are you sure there is multiple jsp is getting dynamically include to your one jsp file.
please check the code may be there is multiple <div> will be there . where while click on checkbox
it will make the <div> visiable...

If this is the case , if you are using multiple <div> and making it enable by clicking on check box then you can do it by javascript.


If this is not the scenario and you are calling the multiple jsp by <jsp:include> on click on checkbox... then javascript will not
help. You have to take a bean class and has to set the value in the jspbean and populate that bean value to the first jsp..

as when it will call first time there is no value in the bean,... when you click the second jsp you have to set the text value to the jspbean and has to call the same page....

while doing this ... the textfield in the first page will get the value from the jspbean .



Please note that keep the scope of the jspbean as session.


Thank you for your inputs. Yes there is <div> in the forms. So it must be done through javascript.
Please do the needful.


Hi Patel,

I have done as below. But Its not populating the vaule in textbox.

In Y.jsp the code is like this.

var f1 = top.document.forms['tform'];
var f2 = top.document.forms['tform1'];

<input type=radio name=data value="data_res" onclick="javascript:getData();/>

on getData() method I have written the below code.

function getData() {

var rd = document.getElementsByName('data');

for(var i = 0; i < rd.length; i++) {
if(rd[i].checked) {
field = parent.document.createElement('INPUT');
field.value = rd[i].value;
document.forms[tform].data_back.value = rd[i].value; // Here data_back is the name of the textbox for form name tform which is the upper form already displayed
(X.jsp)

}
}
f2.submit();
}

The above code is not replacing the selected the text value into the upper form textbox location. Please let me know whether I am missing something.

Baji Roy.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
WHy are you creating form elements? I thought you had form elements on the page you were changing the value of? Why are you not adding the elements you created to the page? They are not attached to the form.

Eric
binayakumar patel
Greenhorn

Joined: Jun 26, 2009
Posts: 27

Hi Patel,

I have done as below. But Its not populating the vaule in textbox.

In Y.jsp the code is like this.

var f1 = top.document.forms['tform'];
var f2 = top.document.forms['tform1'];

<input type=radio name=data value="data_res" onclick="javascript:getData();/>

on getData() method I have written the below code.

function getData() {

var rd = document.getElementsByName('data');

for(var i = 0; i < rd.length; i++) {
if(rd[i].checked) {
field = parent.document.createElement('INPUT');
field.value = rd[i].value;
document.forms[tform].data_back.value = rd[i].value; // Here data_back is the name of the textbox for form name tform which is the upper form already displayed
(X.jsp)

}
}
f2.submit();
}

The above code is not replacing the selected the text value into the upper form textbox location. Please let me know whether I am missing something.

Baji Roy.


I know you have not given the complete code but still concentrate these lines:
var rd = document.getElementsByName('data');
is the data is a TextBox or RadioButton

that you are checking this with an array
rd[i].checked

I am not able to understand here .... Because you are taking the rd value to store in the "data_back"
If it is a checkBox or RadioButton we can check for "checked" or "Unchecked", if it is a TextBox then how you are checking for "checked"

You are creating a new filed and storing the data to it.... But I have not seen any line where you are using "f2" to get data from the textbox of second form.

you are using rd[i].value ... if it is a checkBox or RadioButton then it will return you only true and false.
So no where I found that you are taking the data from the form2 -> textbox

I think this code may help you , Please modify this as per your requirement:
 
GeeCON Prague 2014
 
subject: How to populate a value in text box