Solved

difficulty getting field name from form into javascript function

Posted on 2007-12-05
10
2,517 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
Comment Utility
Correct this line

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

Author Comment

by:frippel
Comment Utility
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
Comment Utility
ok then store it this way:

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

Author Comment

by:frippel
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:frippel
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

762 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

10 Experts available now in Live!

Get 1:1 Help Now