Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 16th, 2006, 8:21 AM   #1
Druid
Programmer
 
Join Date: Mar 2006
Posts: 40
Rep Power: 0 Druid is on a distinguished road
Javascript: form processing, component name - variable

Hello. I am trying to loop through a list of radio buttons (up to 5 per "group"). Depending on which one is checked, I want to append that value to a value string. The problem I have is I don't know how to make the radio button name a variable. I am accessing it by name. This will not work because there will be an unknown number of radio buttons, although there will be five per group name.

I have:

for (var i = 0; i < document.frmOne.rad.length; i++)
{
   if (document.frmOne.rad[i].checked)
   {
       D = document.frmOne.rad[i].value;
   }
}

I need to get "rad" to be a variable name to represent the current radio button group.

Can anyone point me in the right direction?
Druid is offline   Reply With Quote
Old Mar 16th, 2006, 8:58 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Clear a couple of things up. Where will the variable come from and how will its value be set? Server side, as from a file or DB? Client interaction?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 16th, 2006, 9:09 AM   #3
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
Just give each radio button a uniqe ID 'rb_1', 'rb_2', ..., 'rb_n' and
then you can access each one directly with something like:
var D=document.geElementById(frmOne.rad[i].id);
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet"
Agent 47 is offline   Reply With Quote
Old Mar 16th, 2006, 9:21 AM   #4
Druid
Programmer
 
Join Date: Mar 2006
Posts: 40
Rep Power: 0 Druid is on a distinguished road
DaWei:

I have PL/SQL code that generates a HTML form. The form consists of N statements followed by five radio buttons. There is an input button that executes a javascript function during an onClick event.

The javascript function's job is to process all of the input box selections on take each of their values and add them to a string of values... which, in turn, is passed back to the PL/SQL procedure for further processing.


Agent47:

The procedure is generic, to the effect of me not knowing the number of entries. I know there will be five radio buttons per entry, but I'm not sure if I can go this route if the number of entries is unknown. Is this still possible?
Druid is offline   Reply With Quote
Old Mar 16th, 2006, 9:22 AM   #5
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
Can you post the HTML?
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet"
Agent 47 is offline   Reply With Quote
Old Mar 16th, 2006, 9:40 AM   #6
Druid
Programmer
 
Join Date: Mar 2006
Posts: 40
Rep Power: 0 Druid is on a distinguished road
Sure. I stripped out the PL/SQL stuff.

<SCRIPT language = JavaScript>
                                                                                                                                          
          function calculate()
          {
                 for (var i=0; i < document.frmOne.s1.length; i++)
                 {
                    if (document.frmOne.s1[i].checked)
                    {
                         D = document.frmOne.s1[i].value;
                    }
                 }
                                                                                                                                          
                 document.frmOne.valString.value = document.frmOne.valString.value + D;
          }
                                                                                                                                          
    </SCRIPT>


The PL/SQL code generates the form content, but basically it appears like this:

                                                                                                                              
                                                                                                                                          
                                                                                                                                         
    <FORM NAME = frmOne>
                                                                                                                   
    statement1<br>                  
    <input type="radio" name="s1" value="1"> S11
    <input type="radio" name="s1" value="2"> S12
    <input type="radio" name="s1" value="3"> S13
    <input type="radio" name="s1" value="4"> S14
    <input type="radio" name="s1" value="5"> S15
                    
    statement2<br>                                                                                                                      
    <input type="radio" name="s2" value="1"> S21
    <input type="radio" name="s2" value="2"> S22
    <input type="radio" name="s2" value="3"> S23
    <input type="radio" name="s2" value="4"> S24
    <input type="radio" name="s2" value="5"> S25
                                                                                                                                          
                                                                                                                                          
    <P>
    Total: <INPUT TYPE = Text NAME = valString SIZE = 25 value = "">
    <P>

                                                                                                                                              
    <input type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>
    </form>
Druid is offline   Reply With Quote
Old Mar 16th, 2006, 4:45 PM   #7
Druid
Programmer
 
Join Date: Mar 2006
Posts: 40
Rep Power: 0 Druid is on a distinguished road
? anyone ?
Druid is offline   Reply With Quote
Old Mar 16th, 2006, 4:59 PM   #8
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
Sorry, so ... erm... what's the issue? You can have blocks of s1, s2,
..., sn but you don't know what n is, right?
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet"
Agent 47 is offline   Reply With Quote
Old Mar 16th, 2006, 5:23 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Frankly, your requirements still aren't clear, at least to me. Suppose you offered someone a menu for either a 3-course or 5-course menu. Your information could be in a database. Your server side code could generate a page offering either three or five sets of radio buttons giving them their choice of one of five dishes for each course. On the other hand, if you want to generate the whole enchilada with client-side script, that could be done, too. I could make the time to write both kinds of code, but one of those efforts would be wasted, except for general illustrative purposes, right?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC