• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2531
  • Last Modified:

difficulty getting field name from form into javascript function

Yes this is prob. a stupid question but can't figure it out.

i'm creating a Dynamic generated form.
with field and this generated button...
<input onClick='var aar=document.form.sumtot" + row_no +".name; CalculateTotal(aar);' value='calculate' class='type1c' type='button'>

this way is was able to retrieve the form field name

which can be sumtotal1, sumtotal3, sumtotal..., sumtotal999,...

so the whole point is to get the field name to my external java script.

There is would like to do the following
function CalculateTotal(frm) {
    var order_total = 0

    // Run through all the form fields
    for (var i=0; i < document.form.elements.length; ++i) {

        // Get the current field
        form_field = document.form.elements[i]

        // Get the field's name
        form_name = form_field.name

        // Is it a "product" field?
        if (form_name.substring(0,3) == "box") {
       
       
            // Get the quantity
            item_quantity =form_field.value
        item_price = 1
       
                order_total += item_quantity * item_price
        }
       
       
   
       
    }
    document.form.frm.value= order_total.toFixed(2);


--> where frm is actually sumtotal1, or ...2 or ...99 based on the frm value
0
frippel
Asked:
frippel
  • 5
  • 3
1 Solution
 
gops1Commented:
Correct this line

var aar="document.form.sumtot" + row_no +".name"
0
 
frippelAuthor Commented:
that's the good one though... like I said I don't want to get the field value.

I realy want the field name into my javascript.

when i do alert(frm) for e.g

i get sumtot3 ...
0
 
gops1Commented:
ok then store it this way:

var aar=eval("document.form.sumtot" + row_no +".name")
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
frippelAuthor Commented:
no that doesn't do it it gives me erro of oject.

when i add this to my javascript however i get
also an erro where it tel me that sumtot3 is undifined...
0
 
frippelAuthor Commented:
i mean when i replace

function CalculateTotal(frm) {
var order_total = 0
xxo=eval(frm)
for (var i=0; i < document.form.elements.length; ++i) {
form_field = document.form.elements[i]
form_name = form_field.name
 if (form_name.substring(0,3) == "box") {
item_quantity =form_field.value
item_price = 1

order_total += item_quantity * item_price
        }
       
  }
    document.form.xxo.value= order_total.toFixed(2);
0
 
frippelAuthor Commented:
or is there a way i could do this

considering i work with a hidden field in the form.

var abcde = document.banza.dieselx.value;
document.form..abcde.value= order_total.toFixed(2);


still get error that abcde is not defined
0
 
frippelAuthor Commented:
never minf figured it out with a hidden form field after running the function
0
 
gops1Commented:
One thing wrong here is when you are using eval here:

var aar=eval("document.form.sumtot" + row_no +".name")

then you do not have to use eval here

xxo=eval(frm)
0
 
Computer101Commented:
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now