aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes how to identify duplicate form elements with same name in a funcation 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 identify duplicate form elements with same name in a funcation" Watch "how to identify duplicate form elements with same name in a funcation" New topic
Author

how to identify duplicate form elements with same name in a funcation

Ted Addis
Greenhorn

Joined: Nov 19, 2008
Posts: 11
I have multiple price and quantity fields and their total ( price * quantity) in a loop with same form name and I want to calculate the total in each row with onChange event. Since they have same name how do I identify the exact fields. Please see example and give me a hint on how I can grab the exact changed field to calculate the corresponding total. The grand total field is outside the loop and would be the sum of all totals

<%
for (i=0; i<5; i++)
{
%>

<input type="text" name = "price"> <input type="text" name = "quantity"> <input type="text" name = "total">

<% } %>

<input type="text" name="grand_total" >
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61651
    
  67

If they are all the same it's practically impossible to distinguish them. Why don't you give them each a unique id?

P.S. Please avoid showing JSP source in an HTML forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ted Addis
Greenhorn

Joined: Nov 19, 2008
Posts: 11
Okay I thought about your idea but how would I find the corresponding fields.
Say I have a counter in my loop and I attach the counter along with the name.

For example
for price, I would have price<%=counter%> so will come out as price1 , price2, price3, etc..
I can do the same on quantity and get quantity1, quantity2, quantity3, etc..

but then in my javascript function, which is onChange event, if the event happens say on price1, how would I extract quantity1 in the function?

Can you explain..

Thanks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61651
    
  67

Well, you'd need to traverse the siblings to find the one you want. Or, again, use id values and then you can leverage document.getElementById() to directly access the elements.

Or, better yet, agree with me that life is too short to write raw JavaScript, and adopt a library to help with this sort of thing. With a library like jQuery it's almost trivial to fashion a selector that addresses other elements such as siblings.

P.S. Also, scriptlets in your JSP? in 2009?
Ted Addis
Greenhorn

Joined: Nov 19, 2008
Posts: 11
Man,
I appreciate your help, but I haven't gotten an answer to my question. I don't have the luxury of time to learn another library. First of all, you seem to be prone to code style. Can you tell me a true MVC architecture where the V NEVER includes some type of logic? the answer is NO. Now just because it is 2009 doesn't mean all application were born same year. There are many of us who are maintaining older systems with extreme deadlines.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61651
    
  67

You made no indication that this is not new code. (And no, none of my apps contain any business logic in the view. Ever.)

In any case, you've got two answers to your question: traverse the DOM looking for a match on name, or add id values and use document.getElementById().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to identify duplicate form elements with same name in a funcation