Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2003, 02:02 PM
Registered User
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default All My Checkboxes are passed as "on", whether or n

All my checkboxes are passed as "on", whether or not
they are checked.

I have a multi-page form.
One of those pages has 8 checkboxes on it -- each with a different name.

I am not yet up to passing these values to a server -- instead I send the values (via a cookie) to a final "thank you" page, where I display the fields that the user typed in on each page.

My problem is this -- if none of those checkboxes are checked, the value property of each checkbox is seen in the debugger (and via alert boxes) to be set to "on" (even though it was not checked) and the checked property of each checkbox is set to 0 [is 0 = "false" and -1 = "true" in JavaScript?].

I got around this my putting in a series of if statements for each field (very klugy, especially for 8 different fields -- but it worked)

e.g.,

var myForm = document.frmPage3;

if (myForm.skill_com.checked == true)
   myForm.skill_com.value = "yes"
else
   myForm.skill_com.value = "no";

if (myForm.skill_db.checked == true)
   myForm.skill_db.value = "yes"
else
   myForm.skill_db.value = "no";
etc.

I tried using the example below from page 208 of the Wrox Beginning JavaScript book, but it did not work

(in the debugger, when element.type equalled "checkbox" & element.checked was not equal to true, element.value remained as "on", & not "no" -- conversely, when element.checked was equal to true, element.value remained as "on", & not "yes")

var element;
for (var i = 0; i < myForm.length; i++){
   element = myForm[i];
   if (element.type == "checkbox"){
       if (element.checked == true){
           element.value == "yes";
} // end inner-if
       else {
            element.value == "no";
} // end inner-else
} // end outer if
} // end for-loop

There's got to be a better way to do this!
Can someone please help?
Thanks in advance.
 
Old December 9th, 2003, 10:40 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

That's the way it is, the value is constant, it does not depend on the whether the boxes are checked. The sample from Wrox is wrong, whether it is wrong in print or you copied it wrongly I cannot tell :).
The assignment at the end is using equality operator.
Code:
var element;
for (var i = 0; i < myForm.length; i++){
   element = myForm[i];
   if (element.type == "checkbox"){
       if (element.checked == true){
           element.value = "yes";//NOT ==
}   // end inner-if
       else {
            element.value = "no";//NOT ==
}   // end inner-else
}   // end outer if
}   // end for-loop
--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
sessions not being passed Chapter 2 akaplan BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 6 June 27th, 2007 03:28 PM
Objects are always passed by reference or value? deb_kareng C# 2 November 2nd, 2006 03:53 AM
passed in IBM Exams hellblazer Need help with your homework? 0 April 1st, 2006 03:27 PM
How grap the passed value in URL ? method ASP.NET 1.0 and 1.1 Basics 1 June 17th, 2005 08:05 AM
Value in Querystring not being passed u_heet Classic ASP Professional 2 September 16th, 2004 07:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.