I have an agent which generates a page of checkboxes.
I have a javascript function which either checks or unchecks a group of checkboxes at a time.
Basically, if the checkbox is checked, uncheck it otherwise check it. I found the if statement here on notes.net and modified to suit my needs.
Print |if (eval(“f.” + checkName + “.checked”) == true)|
Print |eval(“f.” + checkName + “.checked=false”);|
Print |else|
Print |eval(“f.” + checkName + “.checked=true”);|
Print |}|
checkName is the name if the field that is passed into the javascript function.
I am getting an error. I am not sure what exactly the error is - it displays a Done with a yellow warning sign in the status bar of internet explorer. When I click on it, it says ‘Object expected’
Have I made a mistake with the javascript syntax? Any help would be greatly appreciated. Thanks in advance.
Subject: Selecting / Deselecting Checkboxes using javascript if statement - Code Included
Lisa, first things first. Get Firefox (or Netscape or the Mozilla suite) – not just because it’s a better browser, but because they have a JavaScript Console that will tell you what’s wrong. Absolutely invaluable.
Next, I think you may need to make the JavaScript function a bit more sophisticated. Checkboxes are usually (and supposed to be) multi-valued fields. On the web, that means that there will be several entries with the same name but different values:
First Value
Second Value
Third Value
These have to be treated as arrays. You also have to allow for the possibility that there is only one with a given field name. (By GUI standards, that’s wrong, but it happens too often to ignore.) That means that the function needs to look like this:
function clearCheck(fieldname) {
var checkField = f.elements[fieldname];
if (checkfield.length) {
for (i=0; i<checkField.length; i++) {
if (checkField[i].checked) {
checkfield[i].checked = false;
}
}
else if (checkField.checked) {
checkField.checked = false;
}
}
Note, too, that eval() has been banished.
Subject: RE: Selecting / Deselecting Checkboxes using javascript if statement - Code Included
Thanks Stan.
About using Internet Explorer - unfortunately, that is our corporate standard. I could make a special request, but there’s no guarantee that it will even be taken into consideration.
I know what your saying about the multi-valued fields. My issue though was that each checkbox represents a different document (the fieldname is actually the unique documnet id). Basically, I will place a button on the bottom of the page and when it is clicked it should make a copy of all of the documents that have been selected and tag them with a new company name.
I believe I am in a bit of a pickle…!
Subject: RE: Selecting / Deselecting Checkboxes using javascript if statement - Code Included
Hi Lisa,
Why dont you keep the checkbox names same and store document unique id’s as values. May be that will solve your problem. Instead of writing jS function in your agent, write it in a JS file and call it in your agent. That solves a lot of problems for me atleast.
Hope this helps
Rosie