473,594 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ 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(i Level)
{
document.getEle mentById('radio button')[8-iLevel].checked = true;
// document.all.ra diobutton[8-iLevel].checked = true;
}
which refers to:
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(8)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(7)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(6)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(5)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(4)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(3)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(2)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(1)">
Jul 20 '05 #1
3 36171
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.getEle mentById() 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.getEle mentsByName(). 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(i Level)
{
document.getEle mentById('radio button')[8-iLevel].checked =
true;
// document.all.ra diobutton[8-iLevel].checked = true;
}

<input type="radio" name="radiobutt on" value="radiobut ton"
onClick="JavaSc ript:SetZoomLev el(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.******@blueyo nder.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(i Level)
{
document.getEle mentById('radio button')[8-iLevel].checked = true;
// document.all.ra diobutton[8-iLevel].checked = true;
} which refers to: <input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(8)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(7)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(6)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(5)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(4)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(3)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(2)">
<input type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(1)">


Give each button a unique id. For example,

<input id="zoom8" type="radio" name="radiobutt on"
value="radiobut ton" onClick="JavaSc ript:SetZoomLev el(8)">

document.getEle mentById('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(le vel)
{
iLevel = level;
return Refresh();
}

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

The main page then refers to this function by:

function SetZoomLevel(le vel)
{
sURL = top.mapframe_ie .SetZoomLevel(l evel);
}

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
2837
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 Tkinter import * class GetVariant: def __init__(self): self.root = Tk()
6
11179
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 the radio buttons this way(in a loop): this.groupBox1.Controls.Add(radio); but I can see there only one radio button . How can I see all of them or maybe I should use some kind of radio buttons array first?
2
1850
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 and dynamically create them. For example, like the code (that uses Radio Buttons) below. Can I do this and be able to position them on the form as well (even if
9
5502
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 doesn't have control arrays, so my question is how do I do the equivalent in VB 2005?
7
3975
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 propetry index which is position of this control in the array. Can I do this in asp.net or I must create control array with code?
5
13146
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 collection, as if they were an array of radio buttons ? Basically i want to plonk the radio buttons on a panel then use a for loop to index through the radio buttons with an idenex to check thier state. I want to do it this way becasue i want to...
19
4338
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 document.getElementById() in IE is freezing the browser for about 1 second unlike Firefox. Can anyone suggest any speed improvements for painting data into a large number of cells to speed up the process?
4
2427
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 and click play it goes to the next frame. But the Hotspots just dont work with IE. For your information: This works with other browsers like firefox, safari and Opera. MY HEADER <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
0
7946
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7876
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8003
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8234
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6654
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3897
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2385
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
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1210
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.