/    Sign up×
Community /Pin to ProfileBookmark

How do i check if checkbox doesn’t exist yet

I want to output in an alert that there is no checkboxes available, rather then having to do server side code to hide the links that run functions …

[code]

// check all checkboxes
function checkall() {
var cbCollection = document.forms[‘fmail’].elements[‘mailchecks’];
alert(bCollection.length);
if (cbCollection != “undefined”) {
if(cbCollection.length) {
for ( var i = 0; i < cbCollection.length; i++ ) {
if (cbCollection[i].checked==false) {
cbCollection[i].checked=true;
}
}
} else {
cbCollection.checked=true;
}
}else{
alert(‘nochecks’)
}
}

[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 07.2006 — Try:
<i>
</i>function checkall(){
var cbCollection = document.getElementsByName('mailchecks');
if (cbCollection.length&gt;0) {
........
}
else{
..........
}
}
Copy linkTweet thisAlerts:
@snowboarderauthorAug 07.2006 — Thanks!! That works.

Jason
Copy linkTweet thisAlerts:
@KorAug 07.2006 — I know it works. ?

DOM [B]getElementsByName()[/B] has another great advantage. It is the only reference which works in case of a single checkbox (or a single radio button).

I have not decided if it is a bug or a basic level javascript limitation, but, if a single checkbox (or radio button), the classical way:

document['formname']['elementname'].length

returns undefined, and the element can not be found.
×

Success!

Help @snowboarder 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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