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
Solved

difficulty getting field name from form into javascript function

Posted on 2007-12-05
10
2,522 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

790 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