[Javascript] capturing the ID with radio buttons

Rodney Myers rodney at aflyingstart.net
Sun Sep 16 04:13:36 CDT 2001


Bill,

The first thing I noticed in your sample code that there is no distinguishing
feature between one button and the next - unles you count the surrounding
text.                 I would not!

It would certainly help to have the identifying initials in the value of the
radio button.

eg.

     <tr>
        <td width="5%"><input type="radio" value="MVE|NSW" name="RB" ></td>
        <td width="45%">Murray Valley East            </td>
        <td width="30%">MVE</td>
        <td width="15%">NSW</td>
        <td width ="5%" CLASS="invisible" ID="filter" >7</td>
      </tr>

Then you can pass the radio button array as a whole to a function like this one
that will return the value of the checked option and an empty string if none.
(Be sure that there is no possibility of the database+script generating a single
radio button because that does odd things!)



function getRadioVal(rb){
var L=rb.length;var ret="";
for (var i = 0 ; i< L ; i++)
 { if(rb[i].checked) { ret=rb[i].value; break; } }
return(ret);
}

var fValue = getRadioVal(window.document.radioForm.RB);

With my example code the returned fValue might be  "MVE|NSW".
The vertical bar is inserted to enable you to use string.split("|") to retrieve
the components, though of course any separator would do.

If you were submitting back to the server to request a filtered record set then
you could
either insert the component values into suitably named fields,
or build up a query string and script

window.location.href="myscriptURL"+"?"+"paramName1="+retrievedValue1   etc

depending on what the server side programs can best respond to.

hth

Rodney


Bill Marriott wrote:

> Hi Everyone,
>
> I have a project that needs to capture the current ID (filter value) of the
> record that the user selects using Radio Buttons and store it in a variable.
> The records are presented in a table form like.
>
>      <tr>
>         <td width="5%"><input type="radio" value="V" name="RB" ></td>
>         <td width="45%">Murray Valley East            </td>
>         <td width="30%">MVE</td>
>         <td width="15%">NSW</td>
>         <td width ="5%" CLASS="invisible" ID="filter" >7</td>
>       </tr>
>       <tr>
>         <td width="5%"><input type="radio" value="V" name="RB" ></td>
>         <td width="45%">Murray Valley West            </td>
>         <td width="30%">MVW</td>
>         <td width="15%">NSW</td>
>         <td width ="5%" CLASS="invisible" ID="filter" >8</td>
>       </tr>
>
> I don't know how many rows are going to be presented in the table because
> they are generated on the fly in an Informix tag.
> The form <form name="radioForm"> is wrapped around all the rows.
> Can anyone tell me how I might be able to check if a record has been
> selected and how to capture the selected filter value from the table?
>
> Thanks in advance
>
> Bill
>

--
Shop at ssistant Add-ons and Developer Workshops
http://www.aflyingstart.net/addons/

Enquiries regarding Shop at ssistant Classic training :
Call 01256 880770

Rodney Myers
Based in Oxford, England
Technical Director, Shop at ssistant eCommerce Solutions





More information about the Javascript mailing list