SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change checkbox status?!

    How can I change a checkbox status instead of inserting a value in a text box? As it is now the script below just inserts a 1 or 0 in a textbox.

    This might be easy, but I can't figure it out?
    Code JavaScript:
    if (document.form2.putt0.value===''){
    			  document.form2.g0.value = "0"
    		} else {
    			if (gir0 <= pa0 - 2) {
    				document.form2.g0.value = "1"
    			} else {
    				document.form2.g0.value = "0"
    			}
    		}

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you're looking for the 'checked' property. E.g.

    Code JavaScript:
    document.form2.g0.checked = false; // or "true"
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks... Did the job. Extra question... I'm trying to make this so I only have to change it one place nomatter how many fields I have, but doesn't seem to work... Where is the error?
    Code JavaScript:
    for (i = 0; i <= 17; i++){
    		if (document.form2["putt"+i].value===''){
    			  document.form2["g"+i].value = "0"
    		} else {
    			if (gir[i] <= pa[i] - 2) {
    				document.form2["g"+i].checked = true;
    			} else {
    				document.form2["g"+i].checked = false;			}
    		}
    	}

    Thanks

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    If the value is empty then it won't go in to the part that changes the checkbox. It that the issue you're facing?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep... thats the issue

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Well that's what the code is saying.

    If you remove the first else, both if statements will be processed.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well... When setting it up like this I get an error...
    Code JavaScript:
    for (i = 0; i <= 17; i++){
    		if (document.form2["putt"+i].value===''){
    			  document.form2["g"+i].value = "0"
    		} 
    			if (gir[i] <= pa[i] - 2) {
    				document.form2["g"+i].checked = true; // or "true"
    				//document.form2["g"+i].value = "1"
    			} else {
    				document.form2["g"+i].checked = false; // or "true"
    				//document.form2["g"+i].value = "0"
    			}
     
    	}

    The error is "gir" is not defined? Any idea?

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The idea I have is to figure out just what gir and pa are supposed to be
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the whole function:
    Code JavaScript:
    function gir_calc(){
     
    	var pu0 = validNum(document.form2.putt0.value)
    	var pu1 = validNum(document.form2.putt1.value)
    	var pu2 = validNum(document.form2.putt2.value)
    	var pu3 = validNum(document.form2.putt3.value)
    	var pu4 = validNum(document.form2.putt4.value)
    	var pu5 = validNum(document.form2.putt5.value)
    	var pu6 = validNum(document.form2.putt6.value)
    	var pu7 = validNum(document.form2.putt7.value)
    	var pu8 = validNum(document.form2.putt8.value)
    	var pu9 = validNum(document.form2.putt9.value)
    	var pu10 = validNum(document.form2.putt10.value)
    	var pu11 = validNum(document.form2.putt11.value)
    	var pu12 = validNum(document.form2.putt12.value)
    	var pu13 = validNum(document.form2.putt13.value)
    	var pu14 = validNum(document.form2.putt14.value)
    	var pu15 = validNum(document.form2.putt15.value)
    	var pu16 = validNum(document.form2.putt16.value)
    	var pu17 = validNum(document.form2.putt17.value)
     
    	var s0 = validNum(document.form2.stroke0.value)
    	var s1 = validNum(document.form2.stroke1.value)
    	var s2 = validNum(document.form2.stroke2.value)
    	var s3 = validNum(document.form2.stroke3.value)
    	var s4 = validNum(document.form2.stroke4.value)
    	var s5 = validNum(document.form2.stroke5.value)
    	var s6 = validNum(document.form2.stroke6.value)
    	var s7 = validNum(document.form2.stroke7.value)
    	var s8 = validNum(document.form2.stroke8.value)
    	var s9 = validNum(document.form2.stroke9.value)
    	var s10 = validNum(document.form2.stroke10.value)
    	var s11 = validNum(document.form2.stroke11.value)
    	var s12 = validNum(document.form2.stroke12.value)
    	var s13 = validNum(document.form2.stroke13.value)
    	var s14 = validNum(document.form2.stroke14.value)
    	var s15 = validNum(document.form2.stroke15.value)
    	var s16 = validNum(document.form2.stroke16.value)
    	var s17 = validNum(document.form2.stroke17.value)
     
    	var pa0 = <? echo $p1; ?>;
    	var pa1 = <? echo $p2; ?>;
    	var pa2 = <? echo $p3; ?>;
    	var pa3 = <? echo $p4; ?>;
    	var pa4 = <? echo $p5; ?>;
    	var pa5 = <? echo $p6; ?>;
    	var pa6 = <? echo $p7; ?>;
    	var pa7 = <? echo $p8; ?>;
    	var pa8 = <? echo $p9; ?>;
    	var pa9 = <? echo $p10; ?>;
    	var pa10 = <? echo $p11; ?>;
    	var pa11 = <? echo $p12; ?>;
    	var pa12 = <? echo $p13; ?>;
    	var pa13 = <? echo $p14; ?>;
    	var pa14 = <? echo $p15; ?>;
    	var pa15 = <? echo $p16; ?>;
    	var pa16 = <? echo $p17; ?>;
    	var pa17 = <? echo $p18; ?>;
     
     
    	var gir0 = s0 - pu0;
    	var gir1 = s1 - pu1;
    	var gir2 = s2 - pu2;
    	var gir3 = s3 - pu3;
    	var gir4 = s4 - pu4;
    	var gir5 = s5 - pu5;
    	var gir6 = s6 - pu6;
    	var gir7 = s7 - pu7;
    	var gir8 = s8 - pu8;
    	var gir9 = s9 - pu9;
    	var gir10 = s10 - pu10;
    	var gir11 = s11 - pu11;
    	var gir12 = s12 - pu12;
    	var gir13 = s13 - pu13;
    	var gir14 = s14 - pu14;
    	var gir15 = s15 - pu15;
    	var gir16 = s16 - pu16;
    	var gir17 = s17 - pu17;
     
    for (i = 0; i <= 17; i++){
    		if (document.form2["putt"+i].value===''){
    			  document.form2["g"+i].value = "0"
    		} 
    			if (gir[i] <= pa[i] - 2) {
    				document.form2["g"+i].checked = true; // or "true"
    				//document.form2["g"+i].value = "1"
    			} else {
    				document.form2["g"+i].checked = false; // or "true"
    				//document.form2["g"+i].value = "0"
    			}
     
    	}
    }

    Hope that helps!

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    It does help thanks.

    Variables cannot be accessed in that way. You'll need to create an array called gir, and push the values on to that array.

    For example:

    Code javascript:
    var pa = [];
    pa.push(<? echo $p1; ?>);
    pa.push(<? echo $p2; ?>);
    pa.push(<? echo $p3; ?>);
    pa.push(<? echo $p4; ?>);
    ...
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should this be inside or outside the :

    for (i = 0; i <= 17; i++){

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I don't know what you mean by "outside the :"

    However, I suggest that you use less than gir.length instead the magic number 17

    Code javascript:
    for (i = 0; i < gir.length; i++){
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  13. #13
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still gets the "gir" is not defined??? This doesn't happens if I don't use the

    Code JavaScript:
    for (i = 0; i < gir.length; i++){ }

    If I just use it like this:
    Code JavaScript:
    if (document.form2.putt0.value===''){
    			  document.form2.g0.value = "0"
    		} else {
    			if (gir0 <= pa0 - 2) {
    				document.form2.g0.checked = true; // or "true"
    			} else {
    				document.form2.g0.checked = false; // or "true"
    			}
    		}

    Then I get no errors??? But then I have to make this for every record, and thats not the way... Any idea?

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Are you placing the gir and pa values into an array, as in post #10?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •