Link to home
Start Free TrialLog in
Avatar of lesleint
lesleint

asked on

Javascript Function (Beginner)

I am trying to write a function that I can setoff by an onClick event that would check or uncheck a hidden checkbox if another checkbox is checked.  The function I wrote, untested yet is this:
<script language="JavaScript">
Function chkBox(rID, fileName){
if (document.form1.(rID).checked == True;)
{
document.form1.(fileName).checked == True;
}else{
document.form1.(fileName).checked == False;
}
}
</script>
and I was thinking for the onClick event it would be like this
onClick="chkBox(<%=dB.recid%>,<%=dB.fileName%>)"
Because these values are assinged to each row as the page populates my Repeat Region.


Thanks for your help.

Tom
Avatar of jaysolomon
jaysolomon

<jAy>

Dunno if this will help, but it will check all boxes and uncheck them. You can play with the script to suit your needs.

</jAy>
<jAy>

i guess if you had the script it would help huh? ;-)

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
<!--
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 ;
}
//  End -->
</script>

</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="myform" method="post" action="">
  <p><a href="#" onClick="checkAll(document.myform.list)">Check All</a> <a href="#" onClick="uncheckAll(document.myform.list)">UnCheck
    All</a></p>
  <p>
    <input type="checkbox" name="list" value="checkbox">
    A</p>
  <p>
    <input type="checkbox" name="list" value="checkbox">
    B</p>
  <p>
    <input type="checkbox" name="list" value="checkbox">
    C</p>
  <p>
    <input type="checkbox" name="list" value="checkbox">
    D</p>
</form>
</body>
</html>


</jAy>
i'm not to sure what it is you are trying to do.  you can't have a hidden checkbox, but you can have a hidden variable.  if you wanted to toggle it, you would do something like this:

<script>
  function chk(){
    if(document.tmpForm.hiddenVar.value == 0 ){
     document.tmpForm.hiddenVar.value = 1;
    }
    else{
     document.tmpForm.hiddenVar.value = 0;
    }
     //for testing purposes only
     alert(document.tmpForm.hiddenVar.value);
     }
</script>

<form name="tmpForm">
<input type="hidden" name="hiddenVar" value="0">
<input type="checkBox" name="cbox" onclick="chk()">
</form>

your last statment leads me to believe you might have mutilple checkboxs with the same name.  in that case if you wanted to use one to toggle the other, you would do something like this:

<script>
   function chk(){
     document.tmpForm.cbox[2].checked = true;
     return
   }
</script>

with multiple checkboxs you have an arrya of checkboxs, in the above example, when you execute this function the 2nd(actually it may be the 3rd, i believe javascript starts arrays at 0) check box will become checked.  you can toggle it the same way as in the first example
<jAy>
You can have a hidden check box in a hidden table, as long as the table is in between the<form> tags.


</jAy>
Avatar of lesleint

ASKER

The checkbox is hidden in a div = display none.

When they, the user, clicks on submit what the page will do is send every checked box to a doDelete.asp page.  This page will then take every recid in Request("recid") and delete that record from the dB then it will take every Request("fileName") and delete the actual file.  So when the user clicks on the checkbox, which is "recid", I want it to also select the hidden, the Filename, so that will be sent on the submit.
Changed my code:
function chkBox(recID, flName){
if (document.form1.+recID+.checked == True;)
{document.getElementById.+flName+.checked == True;}else{document.getElementById.+flName+.checked == False;}
}

Here is my onClick:

onClick="chkBox(<%=(rsPDFDel.Fields.Item("recid").Value)%>,'<%=(rsPDFDel.Fields.Item("pdfFilename").Value)%>');return false;"

I get Object Expected
Changed my code:
function chkBox(recID, flName){
if (document.form1.+recID+.checked == True;)
{document.getElementById.+flName+.checked == True;}else{document.getElementById.+flName+.checked == False;}
}

Here is my onClick:

onClick="chkBox(<%=(rsPDFDel.Fields.Item("recid").Value)%>,'<%=(rsPDFDel.Fields.Item("pdfFilename").Value)%>');return false;"

I get Object Expected
Avatar of fritz_the_blank
try this instead:

if(document.forms[0].elements[recID].checked){
     document.forms[0].elements[flName].checked == true;
}else{
     document.forms[0].elements[flName].checked == false;
}

Fritz the Blank
Remember also that JavaScript is case sensitive!

Fritz the Blank
I get this error
docuemnt.forms.0.elements[...].checked is null or not an object
Also my here is the html for the box that is supposed to be getting checked

<input name="filename" type="checkbox" id="<%=(rsPDFDel.Fields.Item("pdfFilename").Value)%>" value="<%=(rsPDFDel.Fields.Item("pdfFilename").Value)%>">

And here is the recid checkebox, which is the one that is being clicked
<input name="recid" type="checkbox" id="recid" value="<%=(rsPDFDel.Fields.Item("recid").Value)%>" onClick="chkBox(<%=(rsPDFDel.Fields.Item("recid").Value)%>,'<%=(rsPDFDel.Fields.Item("pdfFilename").Value)%>');return false;">
I want to make certain that I understand:

both recID and flName are variables that hold the names of fields that exist?

Fritz the Blank
The name property of each checkbox is:
recid

filename

there ID is dynamic
Here is a working example so you can see how it ticks:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function setChecked(strField1,strField2){
     if(document.forms[0].elements[strField1].checked){
          document.forms[0].elements[strField2].checked = true;
     }else{
          document.forms[0].elements[strField2].checked = false;
     }
}
//-->
</SCRIPT>


</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<INPUT type="checkbox" id=checkbox1 name=checkbox1 onClick="setChecked('checkbox1','checkbox2')">
<INPUT type="checkbox" id=checkbox2 name=checkbox2>
</FORM>
</BODY>
</HTML>


Fritz the Blank
You will just have to change the names of the paramaters that you pass.

Fritz the Blank
Fritz will this work if the name and id are not the same.  There are like 100 checkboxes all the same name but each with a different ID.

I just tryed your script and it would just check the one checkbox but then the 4th time I check something it returns the same error as above.

document.forms.0.elements[...].checked is null or not an object
I am pretty sure that JavaScript uses the name rather than the id, so if all of the checkboxes have the same name that might be trouble! For example, in the following, I keep the same name but change the ID, and it will not work:


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function setChecked(strField1,strField2){
     if(document.forms[0].elements[strField1].checked){
          document.forms[0].elements[strField2].checked = true;
     }else{
          document.forms[0].elements[strField2].checked = false;
     }
}
//-->
</SCRIPT>


</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<INPUT type="checkbox" id=checkbox1 name=checkbox1 onClick="setChecked('checkbox1','checkbox2')">
<INPUT type="checkbox" id=checkbox2 name=checkbox1>
</FORM>
</BODY>
</HTML>


Fritz the Blank
Here is the visible checkbox now

<input name="recid" type="checkbox" id="<%=(rsPDFDel.Fields.Item("recid").Value)%>" value="<%=(rsPDFDel.Fields.Item("recid").Value)%>" onClick="setChecked('<%=(rsPDFDel.Fields.Item("recid").Value)%>','<%=(rsPDFDel.Fields.Item("pdfFilename").Value)%>');">
what a fat threat a "beginner's" questions can cause :-)

1. just to mention: some browser DON'T submit INPUT elements having display:none!
NS6 is one of them...
it may work if it's the DIV having display:none and not the INPUT, not sure.

<input name="recid" ...
2. your checkbox will appear as "recid" in your server-script (request object), no matter what the ID is.
ID is for CSS and JS *only*.

3. The form.elements collection takes the name and NOT the id as an index (as mentioned somewhere up here). If elements share the same name, they'll become an array: e.g. elements["recid"][<number>].

Your last code will exactly produce this, that's why form.elements['recid'].whatever MUST fail because elements['recid'] became a pure, native JS array.

ALL examples are using *different* !!!names!!! for the checkbox, and you keep constantly ignoring it and stick with that ID... for whatever reason.
I know PHP can handle "array" elements using name="recid[]" - dunno if ASP provides a similar mechanism.

Fritz' code will work, when changed using your ID.

<copy'n'paste'n'changed>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Fritz the Blank">
<META NAME="AUTHOR" Content="CirTap">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function setChecked(chk,strField2){
  document.getElementById(strField2).checked = chk.checked
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<INPUT type="checkbox" id=checkbox1 name="SHARED_NAME" onClick="setChecked(this,'checkbox2')">
<INPUT type="checkbox" id=checkbox2 name="SHARED_NAME">
</FORM>
</BODY>
</HTML>

CirTap

@fritz: I thought MS supports HTML standards...I'm missing a billion quotes in the Visual Studio code ;->
having all these postings, the following got lost somehow:

> So when the user clicks on the checkbox, which
> is "recid", I want it to also select the hidden,
> the Filename, so that will be sent on the submit.

In case ASP is able to handle "recid" as an Array, you can have the checkbox VALUE be the filename or key that identifies this file. You won't need this hidden checkbox.

<input type="checkbox" name="recid" VALUE="<% recid here %>">
<input type="checkbox" name="recid" VALUE="<% recid here %>">
<input type="checkbox" name="recid" VALUE="<% recid here %>">
<input type="checkbox" name="recid" VALUE="<% recid here %>">

In your ASP page loop throught the recid-Array variable you receive and you have all you need.

CirTap

This is a working PHP version which *requires* the input's name to be suffixed with square brackets [] to work.
For whose who can translate it into ASP.

With file1 and file3 being checked, it will print
Array
(
    [0] => file1.txt
    [1] => file3.txt
)


<html>
<body>
<form action="<?=$PHP_SELF?>" method="POST">
<br/><input type="checkbox" name="recid[]" VALUE="file1.txt">file1.txt
<br/><input type="checkbox" name="recid[]" VALUE="file2.txt">file2.txt
<br/><input type="checkbox" name="recid[]" VALUE="file3.txt">file3.txt
<br/><input type="checkbox" name="recid[]" VALUE="file4.txt">file4.txt
<br/><input type="submit" value="Kick 'em">
</form>
<pre>
<?php
if (isset($_REQUEST) && isset($_REQUEST["recid"])) {
     print_r($_REQUEST["recid"]);
}
?>
</pre>
</body>
</html>
So I could use this?


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Fritz the Blank">
<META NAME="AUTHOR" Content="CirTap">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
function setChecked(strField1,strField2){
    if(document.forms[0].getElementById[strField1].checked){
         document.forms[0].getElementById[strField2].checked = true;
    }else{
         document.forms[0].getElementById[strField2].checked = false;
    }
}
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
 <Fake Repeat Row> <tr>
    <td>
<INPUT type="checkbox" id="recid<%=(rsPDFDel.Fields.Item("recid").Value)%>" name="recid" onClick="setChecked(recid<%=(rsPDFDel.Fields.Item("recid").Value)%>,'filename<%=(rsPDFDel.Fields.Item("pdfFilename").Value)%>')">
<INPUT type="checkbox" id="filename<%=(rsPDFDel.Fields.Item("pdfFilename").Value)%>" name="FileName"></td>
  </tr></Fake Repeat Row>
</table>
</FORM>
</BODY>
</HTML>
It looks like asp code, and to grab the values of the array, you would just do:

arrCheckValues = split(Request.Form("recid"),";")

Fritz the Blank
No, you would have to call it more like this I think (to match your latest post):

onClick="setChecked(this.name,'FileName')"

Fritz the Blank
lesleint,

I tried to "scan" through the ASP stuff.. and NO.
You don't need JavaScript at all!
Please, please give your checkbox a VALUE, and in this value put the recid that tells your serverscript what file to delete.
Because you never provided the VALUE attribute, you had to do this onclick-stuff.

This is how it's supposed to look:
<form action="yourscript.asp" method="POST">
<br/><input type="checkbox" name="recid" VALUE="file1.txt">file1.txt
<br/><input type="checkbox" name="recid" VALUE="file2.txt">file2.txt
<br/><input type="checkbox" name="recid" VALUE="file3.txt">file3.txt
<br/><input type="checkbox" name="recid" VALUE="file4.txt">file4.txt
<br/><input type="submit" value="Kick 'em">
</form>

Your Request object will then contain the VALUES.
The VALUES of the checkboxes that *were actually* selected.
No magic JavaScript - just pure, native, plain HTML.

According to Fritz, you should be able to grab the VALUES using his code.
Here is the thing:

If all of the checkboxes have the same name "recid", then Request.Form("recid") will return a semicolon delimited list of all of the checked values. To access them, you would just need to do this:

arrChecked=Split(Request.Form("recid"),";")

to print them out, all you would have to do is:

for i=LBound(arrChecked) to UBound(arrChecked)
  response.write(arrChecked(i) & "<br>")
next

Fritz the Blank
@fritz: you think Mr. Leslein will believe us? ^5

So this finally turned out to be an ASP/HTML problem, right <gg>?
This sort of "one's moaning he can't get into the car but don't tell people that he's still locked in the house while poeple fiddle around with the car's door" -- too many assumptions.

Have a nice day

[ /me wondering if this Q will end up as a deletion request to CS as well... ]
cirtap,

Don't worry, if it comes to that I will suggest a foce accept to whatever comment above seems the most germaine.

See you about,

Fritz the Blank
The value of recid checkbox is each recordid in a database and the value of filename checkbox is the filename of the file of that record.  I have to have both boxes checked to send those parameters to my doDelete.asp page.  The delete page then says For Each File in Request.Form("filename") delete it and then it says Delete * From table where recid in (Request.Form("recid"))        None of this worked so far and I have just created 2 checkboxes within 2 divs.  When you click on the visible checkbox it hides itself and display the other one already checked then if you uncheck that one it hides itself and and unchecks the other.  I could not figure out how else to do it.  Like I said I am a beginner.
So the recid and filename are two fields in the same record?

Fritz the Blank
Dear Tom,

would you PLEASE try the code we gave you and check what results you have in the Request method?
If you need the record-id of the file, you can make this the VALUE of the checkboxes. On the next roundtrip to the server, you can get all data associated to this Record-ID and display whatever you want.
I don't see the problem and it's NOT JavaScript realted. At all.

CirTap
The point that I am getting at here is that if the recid and the file name are two fields in the same record and if the recid is unique, then there is a much simpler solution.

Fritz the Blank
Yes to Fritz the blank.
I do not understand where this question is heading.  I know how to get my request .forms but both of the checkboxes have to be checked so that there values are sent to me delete page.
Ff the recid and the file name are two fields in the same record and if the recid is unique, then you can do this:

arrRecords = split(Request.Form("recid"),";")

for i=LBound(arrRecords) to UBound(arrRecords)
  strSQL = "DELETE FROM tblTableName WHERE recid = " & arrRecords(i)
  'execute the sql statement
next

Fritz the Blank
That way, you don't need to worry about the filename or anything else.

Fritz the Blank
See the filename though is used in another statement because I am actually deleting files from the server.  I say For Each Filename in Request("Filename") then delete the file
Okay, I see the problem now. Instead of getting that from the request object, why not get it from the record?

for i=LBound(arrRecords) to UBound(arrRecords)
     strSQL = "SELECT filename FROM tblTableName WHERE recid = " & arrRecords(i)
     'do your FSO stuff to delete the file from the server
     strSQL = "DELETE FROM tblTableName WHERE recid = " & arrRecords(i)
     'execute the sql statement to delete the record
next


Fritz the Blank
How would I do this:
SELECT filename FROM tblTableName WHERE recid = " & arrRecords(i)
and have it give me results like this:
a14.pdf, a15.pdf, a16.pdf
Good, so that gives you the name of the file that you wish to delete, right? That should have been the same values that would have been returned to you from the checkboxes named filename!!!

We are getting somewhere  ;-)

Fritz the Blank
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<jAy>

Glad you finally got your answer

Happy Programming


</jAy>
I am glad to have helped, but why the grade of B? Do you not feel that we put a lot of effort into helping you?

Fritz the Blank
this is most unbelievable threat I've ever seen...
I'm glad I went to the movies and had fun instead.

@fritz: Big respect, you kept standing this! The problem wasn't solved the way he wanted it and I'm SURE there are still two checkboxes, and because Mr. Leslein is so in love with them you got this grade.

he was told
...about the source of the JS problem
...how to change the html to get what's required
...how to identify what the user check-marked
...how the data's encoded at the server
...how to handle the data
...a lot more, but obviously he didn't (want to?) listen to what the Experts say.

> I say For Each Filename in Request("Filename") then delete the file

This shows the bad design of the whole application! The data is stored in the wrong place.
I'd love to get access to this form, twaek it and delete a couple of files on the server

Have fun y'all -- finally

CirTap
CirTap,

Thank you for the kind words. It is too bad that lesleint is not as grateful as you are here!!

Fritz the Blank
I told you at the beginning that I am a beginner and if my design is bad so be it I am learning and in the process there will be foul ups.  I gave the grade of "B" because I kept explaining the same thing over and over again to get to a answer that I do not believe to be an "A".  As for CirTap get to my form.  It is me learning Asp and Javascript. Not critical code and if you can get inside my internal 127.0.0.1 network, because it is not on the LAN then so be it delete some files.
Well,

Here is a thought or two for you. Had you explained yourself more clearly, we could have gotten to the heart of the matter more quickly. As for your not getting the answer you were looking for, it could be that your design is flawed and will not work quite as elegantly as you had hoped.

You are, of course, free to grant the grade of B. By the same token, I am free not help out with any future questions that you have.

Fritz the Blank
Hi,

> because I kept explaining the same thing over and over again

Same as we did, Mr. Leslein: explaining over and over again, and you kept ignoring ANY ADVICE we were giving to you, and it appeares (at least to me) that you did not even TRIED to try, test or evaluate a single line of our >solutions<. IF you had, this thread would have been finished around 8:00 PST.

You call yourself a beginner, that's fine -- every expert used to be one;
your application design is probably not very sexy, that's fine, too -- few are;
but to be a beginner or not: if you come to a place like this, asking for help and people DO help you to solve a problem, and after a long track of telling you over and over again how to solve your *REAL* problem (find out what files the user marked for deletion), it's you who is ignorant and this has nothing to do with your skills.

This place is called "Experts Exchange", so who do think is answering? Idiots? Pseudos?
Where do you think Fritz got his 294290 points from? By posting nonsens? Because he's patient? (surely a big advantage for someone who's a "teacher" ;-) )

It's perfectly ok,if you don't understand [everything] of what any expert here is trying to explain, or WHY she/he is given a particular hint/advice/solution. If so: feel free to ask for further informations and you'll usually get a posting explaining the topic or a useful URL for further readings.
It's not ok, practically ignoring anything people telling you in order to HELP you.
You just got stuck with this "idea" of having or needing dynamic IDs in your page, you *wanted* to play with the DOM and JavaScript and synchronize those checkboxes on the client. Maybe you heard of their "power" lately and decided to have them, too. DOM & Co. usually are pretty handy, but they're useless in this particular case.
Go and make your SERVER scripts know what file belongs to which recid and you are fine.

> ...to get to a answer that I do not believe to be an "A".
What do you believe, an "A" graded answer should sound or look like?
If you ask if cows can fly and someone is telling you, they can't, this IS pretty much of a "A"-grade answer, although it might not be the answer YOU wanted or expected.

Programming includes the art of learning, that things don't always work the way we want them to. You'll run into this more often than you want to, when you leave your self-postulated status of a beginner. Murphy's with you.

Facts are: If (at the first place) you were able to write "recid" and "filename" in synch into the page you MUST be as able to retrieve and synch them again using a combination of the ASP and HTML code Fritz and I gave you, having nothing but this damn "recid" array.

Hence the *real* problem was solved, w/o involving JS, something you wanted so bad to be part of the soluting of what YOU though the problem was. Surprise! -- It can be done without!
Even for the JS-part you were given all informations to solve the checkbox-synch. Your example code proves you never made a single appraoch following the suggestions, which supports my theory of your ignorance.

This is making it a total of at least TWO grade "A" answers you got from this thread -- but you preferred not to hear what The Experts told you.


In accordance with Fritz the Blank:
"I am free not help out with any future questions that you have"

Happy learning, Tom, and welcome to world of programming.

CirTap


@Fritz: ^5 && thanx. I'm quite capable to appreciate the help of others -- useful or not. Because I'm aware not to be a Master, I'm always open to new insights, even if I learned things the other way.
CirTap,

Thank you for saying what I had lost the ability to say without blowing my top.

Hope to see you around,

Fritz the Blank