/    Sign up×
Community /Pin to ProfileBookmark

JavaScript: dealing with checkboxes, any ideas?

I’m not with JavaScript, but I have grasped the basic idea of it, however the only problem I have is working with checkboxes.

I have this code (written by myself) that is supposed to check all checkboxes in a form:

<script>
<!–
function checkall(formname, checkAllId, checkid) {
var checkAll = eval(“document.forms.” + formname + “.” checkAllId);
var checkObj = eval(“document.forms.” + formname + “.” + checkid);
var checkedState;
if(checkAll.checked) {
checkedState = 1;
} else {
checkedState = 0;
}
// Loop through all checkboxes
for(i = 0; i < checkObj.length; i++) {
checkObj[i].checked = checkedState;
}
}
//–>
&lt;/script&gt;

and here is the HTML:

&lt;form name= “checkboxgroup”&gt;
<!– Checkbox used to check ALL checkboxes in our form –>
<input type= “checkbox” name= “select1” onClick= “checkall(‘checkboxgroup’, ‘select1’, ‘cb’)” />Check or uncheck all?<br />
<input type= “checkbox” name= “cb” /><br />
&lt;/form&gt;
<!– END CODE –>

That is just a small example.
I know what I am doing, yet I dont. ?
What am I doing wrong, what could I possibly do to fix this?

Thanks! Any help is greatly appreciated! ?
— John

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@felgallJun 04.2009 — The problem is that you are trying to loop through an array called checkObj when that is a single variable and not an array. You are also making the code a lot messier than it needs to be by using eval. Exactly how to fix the code depends on how you are defining all the checkboxes to be checked/unchecked (since your sample code only shows one) but the simplest solution is to prehaps loop through all the form fields and update all the checkboxes except the one that is controlling it all.

Instead of

var checkAll = eval("document.forms." + formname + "." checkAllId);


(which is missing a + anyway and therefore wouldn't work) you should use

var checkAll = document.forms[formname].checkAllId;

For the loop I'd use:

for(i = document.forms[formname].elements.length - 1; i &gt;= 0; i--) {
if (document.forms[formname].elements[i].type == 'checkbox' &amp;&amp;
document.forms[formname].elements[i].name != checkAllId)
document.forms[formname].elements[i].checked = checkedState;
}
×

Success!

Help @johnnn 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.16,
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,
)...