Solved

difficulty getting field name from form into javascript function

Posted on 2007-12-05
10
2,519 Views
Last Modified: 2008-02-21
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
Comment
Question by:frippel
  • 5
  • 3
10 Comments
 
LVL 17

Expert Comment

by:gops1
ID: 20411193
Correct this line

var aar="document.form.sumtot" + row_no +".name"
0
 

Author Comment

by:frippel
ID: 20411234
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
 
LVL 17

Expert Comment

by:gops1
ID: 20411444
ok then store it this way:

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

Author Comment

by:frippel
ID: 20412218
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:frippel
ID: 20412264
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
 

Author Comment

by:frippel
ID: 20413188
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
 

Author Comment

by:frippel
ID: 20413318
never minf figured it out with a hidden form field after running the function
0
 
LVL 17

Accepted Solution

by:
gops1 earned 100 total points
ID: 20413678
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
 
LVL 1

Expert Comment

by:Computer101
ID: 20953169
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now