Solved

difficulty getting field name from form into javascript function

Posted on 2007-12-05
10
2,524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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
 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

729 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