• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ted Addis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ted Addis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic