/    Sign up×
Community /Pin to ProfileBookmark

Help with turning all the checkboxes in a certain form checkedclear

Hello,
I use the following syntax for a function which receives an HTML element name and turns all the checkboxes that named after this name checkedclear if it’s a checkbox.

[QUOTE]

function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}

[/QUOTE]

I call the functions when the users presses the following buttons:

[code=html]
<tr><td><input type=”button” value=”Check all” name=”checkall” onClick=”checkAll(document.document.aut)”></td><td><input type=”button” value=”Uncheck all” name=”uncheckall” onClick=”uncheckAll(document.document.aut)”></td></tr>
[/code]

It works just fine when i use the following syntax for the checkboxes:

[code=html]
<tr><td><input type=”checkbox” name=”aut” value=”1″></td><td></td></tr>
[/code]

But when i add ‘[]’ at the end of the names, and at the parameter that i send to the function it sises to work…
I use PHP to process this form and i have to use square brackets to automatically order all the values of the checked checkboxes in a array.
Here the source that doesn’t works looks…

[code=html]
<tr><td><input type=”checkbox” name=”aut[]” value=”1″></td><td><td></tr>
<tr><td><input type=”button” value=”Check all” name=”checkall” onClick=”checkAll(document.document.aut)”></td><td><input type=”button” value=”Uncheck all” name=”uncheckall” onClick=”uncheckAll(document.document.aut)”></td></tr>
[/code]

I’ll highly appreciate any help…

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 25.2006 — You should use square brackets notation

document['formname']['elementname']

or DOM

document.getElementsByName('elementname')

to be able to use dinamically the reference:

[code=php]
<script type="text/javascript">
function checkUncheck(n,b){
var c = document.getElementsByName(n);
for(var i=0;i<c.length;i++){
c[i].checked=b
}
}
</script>
...
<input type="button" value="Check all" name="checkall" onclick="checkUncheck('aut[]',true)">
<input type="button" value="Uncheck all" name="uncheckall" onclick="checkUncheck('aut[]',false)">
[/code]
Copy linkTweet thisAlerts:
@Mr_JJan 25.2006 — Please see if this help you out

[code=php]<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script type="text/javascript">

function checkAll(el){
myForm=document.forms["f1"]

for (var i = 0; i < myForm.elements[el].length; i++){
myForm.elements[el][i].checked = true
}

}


function uncheckAll(el){
myForm=document.forms["f1"]

for (var i = 0; i < myForm.elements[el].length; i++){
myForm.elements[el][i].checked = false
}

}

</script>
</HEAD>
<BODY>

<form name="f1">
<input type="button" value="Check all" name="checkall" onclick="checkAll('aut[]')">
<input type="button" value="Uncheck all" name="uncheckall" onclick="uncheckAll('aut[]')">

<BR><BR>

<input type="checkbox" name="aut[]" value="1"><BR>
<input type="checkbox" name="aut[]" value="1"><BR>
<input type="checkbox" name="aut[]" value="1"><BR>
<input type="checkbox" name="aut[]" value="1"><BR>

</form>

</BODY>
</HTML>[/code]


Sorry Kor, didn't see that you'd already replied
×

Success!

Help @HellgY 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 5.6,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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