473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RadioButtonList - OnClick Issue

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

<table id="optMedTreat ment"
onClick="SetFoc usDayPhone(docu ment.getElement sByName('optMed Treatment'))">
<tr>
<td><input id="optMedTreat ment_0" type="radio" name="optMedTre atment"
value="Yes" /><label for="optMedTrea tment_0">Yes</label></td>
</tr><tr>
<td><input id="optMedTreat ment_1" type="radio" name="optMedTre atment"
value="No" /><label for="optMedTrea tment_1">No</label></td>
</tr>
</table>

function SetFocusDayPhon e(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 13961
di**********@gm ail.com wrote:
OK, I am checking the values of a radiobuttonlist with 2 values (Yes
and No) using javascript:

<table id="optMedTreat ment"
onClick="SetFoc usDayPhone(docu ment.getElement sByName('optMed Treatment'))">
If you put the radio buttons in a form, you can use:

<form action=""
onclick="SetFoc usDayPhone(this .optMedTreatmen t);">
<table id="optMedTreat ment">
...
</table>
</form>
Which is shorter and more widely supported than getElementsByNa me. The
difference is that getElementsByNa me 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 "optMedTreatmen t").

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="optMedTreat ment_0" type="radio" name="optMedTre atment"
value="Yes" /><label for="optMedTrea tment_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="optMedTreat ment_1" type="radio" name="optMedTre atment"
value="No" /><label for="optMedTrea tment_1">No</label></td>
</tr>
</table>

function SetFocusDayPhon e(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="optMedTreat ment_1" type="radio" name="optMedTre atment"
value="No"
onclick="SetFoc usDayPhone(this );">
Then the function is simply:

function SetFocusDayPhon e(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.lengt h" above.
--
Rob
Jan 24 '06 #2

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

Similar topics

2
10569
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 checkbox is checked, then I want the radiobuttonlist to appear. I want to use javascript. On Form_Load I've done: chkM.Attributes.Add("onclick",...
5
5501
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 -- -- BoundColumn 1 -- -- BoundColumn 2 -- -- TemplateColumn 4 -- -- RadioButtonList --
2
5975
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 need the clientside script to run when the selected radio button changes, but I'm having trouble registering onclick events with each of the radio...
0
2202
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 its excellent ability to be bound to a datasource. No matter what I do though, I am not able to add an onclick event to the individual radio buttons...
1
3449
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 textbox should be disabled. And i need to handle this in client side only. i'm not able to call a javascript function for radiobuttonlist. can...
5
2684
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 running this in IE, behaviour is as I would expect it. If I select an item and do a postback, the page remembers my selection when reloading, and the...
6
2631
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 in the page and I've got code to get the selected value from it in the Radiobuttonlist_SelectedIndexChanged event. The code in there is: ...
1
10754
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 RadioButtonList (for options). The 2nd Template field is the primary key value. And i have a button on the form. In the button click event i wrote code to...
13
10707
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 static controls. However I would like to move on to attempt to create some dynamic controls. So I set out to work with a radiobuttonlist...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.