/    Sign up×
Community /Pin to ProfileBookmark

What can I do to fix this?

I get an error in the FF javascript: console with this code. ?

[code=php]
<html>
<head>
<title>Radio Button Test</title>
<script type=”text/javascript”>
<!–
function getSWKrb() {
var len = document.getElementById(‘SWK’).length;
for (var i=0; i<len; i++) { if (document.getElementById(‘SWK’)[i].checked) { break } }
return document.getElementById(‘SWK’)[i].value;
}
//–>
</script>
</head>

<body>
<h2>Radio Button Test</h2>
<p />

<script type=”text/javascript”>
<!–
document.write(“<br />For: Week #”);
for (i=1; i<20; i++) {
var s = “<input type=’radio’ id=’SWK’ name=’SWK’ value='”+i+”‘”;
if (i==1) { s += ” checked”; }
document.write(s+’>’+i);
}
//–>
</script>
<p />
<button id=’btn’ onClick=”document.getElementById(‘showRBset’).value=getSWKrb()”>Get setting</button>
<input type=”text” id=”showRBset” value=””>
</body>
</html>
[/code]

What can I change to avoid the dreaded:
document.getElementById(“SWK”)[i] has no properties error in Line 9?

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzAug 10.2006 — [url=http://www.w3.org/TR/html401/struct/global.html#adef-id]IDs should be unique.[/url]

Use document.getElementsByName() instead of document.getElementById().
Copy linkTweet thisAlerts:
@vwphillipsAug 10.2006 — [CODE]<html>
<head>
<title>Radio Button Test</title>
<script type="text/javascript">
<!--
function getSWKrb() {
var len = document.getElementsByName('SWK').length;
for (var i=0; i<len; i++) { if (document.getElementsByName('SWK')[i].checked) { break } }
return document.getElementsByName('SWK')[i].value;
}
//-->
</script>
</head>

<body>
<h2>Radio Button Test</h2>
<p />
document.getElementById() does not return a collection
<script type="text/javascript">
<!--
document.write("<br />For: Week #");
for (i=1; i<20; i++) {
var s = "<input type='radio' id='SWK' name='SWK' value='"+i+"'";
if (i==1) { s += " checked"; }
document.write(s+'>'+i);
}
//-->
</script>
<p />
<button id='btn' onClick="document.getElementById('showRBset').value=getSWKrb()">Get setting</button>
<input type="text" id="showRBset" value="">
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorAug 10.2006 — Thanks to both of you. ?

I totally missed the difference between: ? (I must be blind)

- getElementById()

- getElementsByName()

Are there any rules as to which should be used in a particular situation?

Are there any other similiar functions that I need to watch for? :o

Also, are there any functions named:

- getElementsById() ?

or

- getElementByName() ?

that I also don't know anything about?

Sorry for so many questions. :rolleyes:
Copy linkTweet thisAlerts:
@KravvitzAug 10.2006 — Are there any rules as to which should be used in a particular situation?[/quote]
Use getElementsByName() when the elements have names.

Use getElementById() when the element has an ID.

Are there any other similiar functions that I need to watch for?[/quote]
There is getElementsByTagName().

Also, are there any functions named:

- getElementsById() ?[/QUOTE]

No, because like I said, IDs should be unique.

- getElementByName() ?[/QUOTE]
This would be useless since there is getElementsByName() which works fine if only one element has a particular name.
Copy linkTweet thisAlerts:
@JMRKERauthorAug 10.2006 — Final questions (hopefully):

Does it matter which I use to get the most cross-browser compatibility

or is it just a matter of being more consistent of the use of the functions

when I create the scripts?

Am I better using 'id=' or 'name=' for a majority of the scripts?

Any advantage or disadvantage to either use of the labels?

As long as I make unique 'id=' (and 'name=' entries),

they can be the same (ie, id='SWK' name='SWK') in the function definitions?

So many questions ... So little time! Just trying to make sure I understand.

Thanks again for your time.
Copy linkTweet thisAlerts:
@JMRKERauthorAug 13.2006 — Been several days since my last question,

so I though I'd try once more for clarification.

Thanks for ANY answers.
Copy linkTweet thisAlerts:
@konithomimoAug 13.2006 — Choosing whether to use IDs or NAMEs is dependent on your situation. Objects such as radio groups or checkbox groups can have the same name, but not the same ID. IDs should always be unique, where as names should always be unique unless you are trying to create a group. As far as cross browser compatibility, NAMEs are supported in all browsers, where as IDs are not. Technically you can access an element by its name using, getElementByName('desiredname'), but that can produce undesirable results in IE since IE will check the ID and Name of the object. To get around this you can do getElementsByName('desiredname')[0], but getElementsByName() is usually usesd for grouped objects such as radio buttons or checkboxes.
Copy linkTweet thisAlerts:
@KravvitzAug 13.2006 — Any browser that supports getElementById() should support getElementsByName() and vice versa.

Most elements can have an ID attribute. Only a few can have a NAME attribute.

<input> elements that aren't type="button" or type="submit" need a NAME attribute. The ID is optional but is often useful.

As long as I make unique 'id=' (and 'name=' entries),

they can be the same (ie, id='SWK' name='SWK') in the function definitions?[/quote]

Yes, the ID and NAME can be set to the same thing on an element.

As far as cross browser compatibility, NAMEs are supported in all browsers, where as IDs are not.[/quote]
As far as I know, only antique browsers like Netscape 4 don't fully support the ID attribute.
Copy linkTweet thisAlerts:
@JMRKERauthorAug 13.2006 — konithomimo and Kravvitz,

Thanks to you both for the clarifications.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...