my dog learned polymorphism*
The moose likes Struts and the fly likes struts and java script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts and java script" Watch "struts and java script" New topic

struts and java script

vidya mahavadi
Ranch Hand

Joined: Nov 24, 2004
Posts: 34

I have a jsp where I am displaying a table with some transactions and next to each transaction I have a checkbox. I got my jsp working.. But I also need to add a small java script to add the amount in that transaction row to a text feild (another form field). Here is the code snippet..

public class PolicyTrnsDisplayObject implements Serializable{

private PolicyTrns policyTrns;
private boolean checked;

//getters and setters are also included

public class AdjustmentRequestActionForm extends ActionForm {
private List policyTransactions = new ArrayList();
//list of displayObjects.

//getters and setters are also included

in jsp,

< html:text name="adjustmentRequestForm" property="amount" />

< logic:iterate id="policyTrnsDisplayObject" name="adjustmentRequestForm" property="policyTransactions" type="PolicyTrnsDisplayObject" indexId="ctr" />

...transaction parameters included here

< html:text name="policyTrnsDisplayObject" property="policyTrns.amount" />

< html:checkbox property='< %= "policyTrnsDisplayObject[" + ctr + "].checked"% >' on/click="" /<

< logic:iterate />

What I need to do is, pass the amount in that row to the javascript! I appriciate any idea to get to work..

Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi vidya,
You can use a id against the table u are using to display the result.Then pass this id in the JS Function.
Get the no of rows for the id(No of rows u are displaying ur trans amt).Then loop thru the no of rows as applicable
and use document.getElementsByName() to get the value of ur field.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There are a couple of ways to do this:

1. Use the Document Object Model (DOM) in JavaScript to navigate to the text input and get it's value. For example, if this is in a table with 3 columns, and the amount is in the first column and the checkbox is in the third column, you could write something like this:


myFunction - some function you write that takes the amount as a parameter.
this.parentNode - gets the cell containing the checkbox
.parentNode - gets the current row
.cells[0] - gets the first cell in the row
.firstChild - gets the first child node inside the first cell
.value - assumes you found the <input> control and gets it's value.

2- you can assign an ID to the <html:text> tag. Then you specify a "styleId" tag in the <html:text> tag, it is rendered as an ID tag on the actual <input> html tag. You can then retrieve the element using its ID with the document.getElementById() JavaScript function.
[ May 15, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
vidya mahavadi
Ranch Hand

Joined: Nov 24, 2004
Posts: 34
Thanks Merril,

Your suggestion worked for me with little modificatiions, using it like..


Thanks for the hint.
I agree. Here's the link:
subject: struts and java script
Similar Threads
struts and java script
how to use indexid of logic:iterate tag in struts
using indexId
Generate Radio Buttons
Indexed Properties with List of Strings