473,486 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

assistence needed getElementById + array for radiobutton

Hello all,

I use the following javascript function in a html document to set the
level of one of eight available radiobuttons. The line that's
commented out works fine under IE, but I need to rewrite it so that
it's W3C compliant. Reading through earlier messages I concluded I
should go with getElementById but this doesn't work. What am I doing
wrong?

Your help is much appreciated.

Sofie

function SetRadioLevel(iLevel)
{
document.getElementById('radiobutton')[8-iLevel].checked = true;
// document.all.radiobutton[8-iLevel].checked = true;
}
which refers to:
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(8)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(7)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(6)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(5)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(4)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(3)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(2)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(1)">
Jul 20 '05 #1
3 36156
sofie wrote on 01 Dec 2003:
Hello all,

I use the following javascript function in a html document to
set the level of one of eight available radiobuttons. The line
that's commented out works fine under IE, but I need to rewrite
it so that it's W3C compliant. Reading through earlier messages
I concluded I should go with getElementById but this doesn't
work. What am I doing wrong?
What you have written below shouldn't work in any browser.
document.getElementById() is supposed to return a single element. IDs
are unique in a document, so there shouldn't be any possibility of
returning more than one. Also, it should only return the element with
a matching id attribute - name shouldn't even be considered. I just
wanted to point that out...

The fix is /really/ simple: use document.getElementsByName(). This is
designed to return a collection of all elements that have a matching
name attribute. However, be aware: the returned elements can appear
anywhere in the document. You can't get elements from a specific
scope. Make sure that the group returned can only be a unique group.
That is, the 'SetZoomLevel' group below should be the only controls
anywhere in your document with the name 'radiobutton'.
function SetRadioLevel(iLevel)
{
document.getElementById('radiobutton')[8-iLevel].checked =
true;
// document.all.radiobutton[8-iLevel].checked = true;
}

<input type="radio" name="radiobutton" value="radiobutton"
onClick="javascript:SetZoomLevel(8)">


A question: why are you using the JavaScript protocol in an intrinsic
event? That's supposed to only be used as a URI specifier. To declare
what language intrinsic events use, you must (that is, it is
*mandatory*) specify the default language using either a META element
or a HTTP header:

<META http-equiv="Content-Script-Type" content="text/javascript">

Without it, your HTML document is invalid, and depends on unreliable
browser behaviour. Browsers don't have to execute intrinsic events if
a default language isn't specified.

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #2
> I use the following javascript function in a html document to set the
level of one of eight available radiobuttons. The line that's
commented out works fine under IE, but I need to rewrite it so that
it's W3C compliant. Reading through earlier messages I concluded I
should go with getElementById but this doesn't work. What am I doing
wrong? function SetRadioLevel(iLevel)
{
document.getElementById('radiobutton')[8-iLevel].checked = true;
// document.all.radiobutton[8-iLevel].checked = true;
} which refers to: <input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(8)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(7)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(6)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(5)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(4)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(3)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(2)">
<input type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(1)">


Give each button a unique id. For example,

<input id="zoom8" type="radio" name="radiobutton"
value="radiobutton" onClick="javascript:SetZoomLevel(8)">

document.getElementById('zoom' + iLevel).checked = true;

http://www.crockford.com
Jul 20 '05 #3
Thank you for your reply! I think I can work out that one with these
tips. I hop e you don't mind if I post another one straight away? My
previous question, as well as this one refer to a GIS website whereby a
map is loaded into an iframe.

The main window contains numerous (radio)buttons used for panning,
zooming etc on a map located in an iframe.

This was all working fine under IE, but NS and Mac won't have it. My
initial code was as follows:

The iframe src-document zoomlevel function:

function SetZoomLevel(level)
{
iLevel = level;
return Refresh();
}

whereby iLevel is a parameter stored in the <head>.

The main page then refers to this function by:

function SetZoomLevel(level)
{
sURL = top.mapframe_ie.SetZoomLevel(level);
}

Does this seem like a logical solution, and if so, what do I use to
replace 'top.' to make it W3C compliant?

thanks again,
Sophie

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4

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

Similar topics

8
2826
by: VK | last post by:
Hi! What I'm missing in following code? Cannot get the values of radiobuttons. Starting only one class (GetVariant), it works. When I put two classes together, it doesn't. Regards, VK from...
6
11168
by: juli | last post by:
Hello, I need a control which will contain radio buttons that will be added in a loop. I am using this control a source of some other control. I tried to use group box windows control and to add...
2
1834
by: Spurry Moses | last post by:
Hi, I want to create an array of combo boxes on my form. The only problem is that I don't want to place 3 seperate combo boxes on the form if I can avoid it. I'd just like to create an array...
9
5478
by: Michael D. Ober | last post by:
In VB 6, you can create control arrays for your option groups and scan with the following code dim opt as OptionButton for each opt in OptionGroup ' Do something next opt I know VB 2005...
7
3966
by: Igor | last post by:
Can I create control array (like TextBox array) in design mode. In vb6 I drow some control at the form and then I copy/paste controls. After that every control have the same name, but they have...
5
13129
by: Peted | last post by:
I know you can iterate through a collection of radio buttons in a panel, using a "for each in control" type iteration that c# supports, but is it possible to iterate through the radio buttons...
19
4311
by: vunet.us | last post by:
Hello, My AJAX application paints data into about 500 cells with unique ID every 10 seconds. I am using document.getElementById() to find the right cell. However, I have noticed that...
4
2402
by: dupdupdup | last post by:
Hello there, im needed to develop a gallery for this website. im using hotspots to go to my frames in my flash Each hotspot goes to each frame. The flash is loaded properly. When i right click...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
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...
1
4863
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...
0
4559
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...
0
3066
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
259
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...

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.