Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick Script for a Wiz PLEASE!

Status
Not open for further replies.

caddmngr

Technical User
Apr 16, 2003
1
US
hi...

Im creating a feedback form with questions that have answers as 3 ratings, and the ratings need to be added together for each question. I dont want to write code for each question, so how can I do this?

here is an example question - Im sure one of you wizzes can answer this in your sleep! Can ya share a quick function with me please?

thanks so much!
chris
 
Hi,

haven't tested this, but have had to use something similar on a really big form recently.
You need to use a particular naming structure for your fields. Call the text boxes "Q1" - change the 1 to the right question number each time - then call your drop down boxes
"Q1a" "Q1b" "Q1c".

It's easier to make reusable code then. Stick this lot in a
function and call it onChange:

Code:
for (i=0; i<formname.length; i++)
 {
  if (formname.elements[i].type == &quot;text&quot;)
   {
    str = formname.elements[i].id;
    str1 = eval (&quot;formname.&quot; + str + &quot;a&quot;);
    str2 = eval (&quot;formname.&quot; + str + &quot;b&quot;);
    str3 = eval (&quot;formname.&quot; + str + &quot;c&quot;);
    formname.elements[i].value = str1.selectedIndex.value
      + str2.selectedIndex.value 
      + str3.selectedIndex.value;
   }
  }


If you want to use other text boxes in the form that
aren't linked to select fields, do

Code:
if (formname.elements[i].type == &quot;text&quot;) 
      && (formname.elements[i].id !== &quot;elementname&quot;)

You also need to remember that 'id' only works in Internet
Explorer, if you want it to work in Netscape etc. as well,
you have to write a second version using 'name' instead of
'id', and use a browser sniffer to see which version the
client is using. (Make sure you put both name and id in the
element tags in this case).

Hope that helps, if there are any bugs, it's likely to be
with the value of your select elements (drop down boxes) not being recognised as a number. In any case, post back if
there are problems, and I'll try to sort them out!

fingerprint


a posse ad esse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top