473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

RadioButtonList - OnClick Issue

OK, I am checking the values of a radiobuttonlist with 2 values (Yes
and No) using javascript:

<table id="optMedTreatment"
onClick="SetFocusDayPhone(document.getElementsByNa me('optMedTreatment'))">
<tr>
<td><input id="optMedTreatment_0" type="radio" name="optMedTreatment"
value="Yes" /><label for="optMedTreatment_0">Yes</label></td>
</tr><tr>
<td><input id="optMedTreatment_1" type="radio" name="optMedTreatment"
value="No" /><label for="optMedTreatment_1">No</label></td>
</tr>
</table>

function SetFocusDayPhone(e) {

var SelectedValue="";
var i=0;

for (i=0; i<=e.length; i++)
{
if (e[i].checked)
{
SelectedValue=e[i].value;
if (SelectedValue == "No")
{
alert("Hell Yeah");
break;
};
};
};
}

I am having 2 issues:

1. When I initially come to the page and check the No option I get the
Hell Yeah message. If I initially come in and check the Yes option I
don't get the message. The problem is that if I check No, get the
message, then check Yes, I still get the message. The javascript is
firing before the value has been changed. How do I get around this?

2. I am getting a error on my page when I check the yes option, this
does not happen when I check the No option as long as it is selected
first (i.e. if the yes is selected and then the No I still get the
error). Error Message is: 'checked' is null or not an object.

Jan 24 '06 #1
1 13936
di**********@gmail.com wrote:
OK, I am checking the values of a radiobuttonlist with 2 values (Yes
and No) using javascript:

<table id="optMedTreatment"
onClick="SetFocusDayPhone(document.getElementsByNa me('optMedTreatment'))">
If you put the radio buttons in a form, you can use:

<form action=""
onclick="SetFocusDayPhone(this.optMedTreatment);">
<table id="optMedTreatment">
...
</table>
</form>
Which is shorter and more widely supported than getElementsByName. The
difference is that getElementsByName will *always* return a nodeList,
whereas getting references using the forms collection may return a
nodeList (as in this case) or a single element (say if there was only
one radio button named "optMedTreatment").

The problem with putting the onclick on the form or table is that a
click *anywhere* on either of them will cause the onclick to fire. If
'No' is checked, you will get the message wherever you click on the form
or table.

<tr>
<td><input id="optMedTreatment_0" type="radio" name="optMedTreatment"
value="Yes" /><label for="optMedTreatment_0">Yes</label></td>
Please don't use tabs when posting code, use two (preferred) or four
spaces for indenting and block code properly to make life easier for
those who might wish to help.

</tr><tr>
<td><input id="optMedTreatment_1" type="radio" name="optMedTreatment"
value="No" /><label for="optMedTreatment_1">No</label></td>
</tr>
</table>

function SetFocusDayPhone(e) {

var SelectedValue="";
Variables with a capital letter as the first character are normally
constructors, not simple local variables.

var i=0;

for (i=0; i<=e.length; i++)
e is a nodeList, its length is always one more than the highest index.
Here the length is 2, the highest index is 1. If the 'break' doesn't
end execution (i.e. when neither radio button is checked), when you try
to evaluate e[2] below, you get an error because you are trying to find
the checked property of an element that doesn't exist.

It is also normal to declare variables inside the for statement rather
than outside:

for (var i=0; i<e.length; ++i)

{
if (e[i].checked)
{
SelectedValue=e[i].value;
if (SelectedValue == "No")
{
alert("Hell Yeah");
break;
};
Don't put semi-colons after the closing brace of an 'if' block, it is
evaluated as an empty statement. It won't cause errors, it's just a bit
ugly.

};
};
}

I am having 2 issues:

1. When I initially come to the page and check the No option I get the
Hell Yeah message. If I initially come in and check the Yes option I
don't get the message. The problem is that if I check No, get the
message, then check Yes, I still get the message. The javascript is
firing before the value has been changed. How do I get around this?
You have put the onclick on the table, not the element that you really
want to check. Why not just put the onclick on the 'no' button? Then
you have:

<input id="optMedTreatment_1" type="radio" name="optMedTreatment"
value="No"
onclick="SetFocusDayPhone(this);">
Then the function is simply:

function SetFocusDayPhone(el)
{
// el is a reference to the 'No' radio button
if (el.checked){
// 'No' is checked, do stuff
}
}


2. I am getting a error on my page when I check the yes option, this
does not happen when I check the No option as long as it is selected
first (i.e. if the yes is selected and then the No I still get the
error). Error Message is: 'checked' is null or not an object.


That is from the erroneous use of "i<=e.length" above.
--
Rob
Jan 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sramruttun | last post by:
hi I have a checkbox and a radiobuttonlist (the radiobuttonlist contains 2 items) in my form. The radiobuttonlist has its visible property set to false at design time. At run time, when the...
5
by: DotNetGruven | last post by:
Anyone have any pointers on how to set the Value and Selected attributes in a ListItem in a RadioButtonList that is in a DataGrid? Here's what I have ------DataGrid------ -- BoundColumn 0 --...
2
by: Helen | last post by:
I've got some clientside script that runs when the selected index of a select box changes. Now I need to swap my selectbox with an ASP.NET radiobuttonlist (to solve some layout issues). I still...
0
by: Ryan Taylor | last post by:
Hello. I am having another issue. I need to execute some JavaScript whenever a radio button is clicked. I am currently using a RadioButtonList control to generate the radio buttons because of...
1
by: novice_developer | last post by:
Hi All, there is a radiobuttonlist having 2 list items (state & zipcode). when i select state radiobutton the zipcode textbox should be disabled and when i select a zipcode radiobutton the state...
5
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hallo, I have a radiobuttonlist control that is added on a custom Web User Control. This control has a property that exposes the SelectedIndex property of the embedded radiobuttonlist. When...
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
1
by: renuami | last post by:
Hello friends please advise....... I am building an application for Survey. For this i have GridView with two Template fields. The first Template has Label (To display question text) and...
13
by: tommymo | last post by:
Hi everyone I'm new to this site and the world of ASP.Net C# programming. I have been learning controls and integrating them with a SQL database. So far I have been able to move along and understand...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.