Forum Moderators: open

Message Too Old, No Replies

changing a hidden value with a checkbox

         

fintan

3:46 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Hi I have a little script that changes the value of a hidden field when someone ticks a checkbox. I can get it to change the value from 0 to 1 but can't get it to change back to 0.
So if someone unticks the checkbox it doesn't change back. It must be something I'm not seeing. Here's what I got so far.

function changeon(){
var chkbox = document.forms[0].photos.value;
var hidden = document.forms[1].hphotos;

if(chkbox.checked = true){
hidden.value='1';
}
}

function changeoff(){
var chkbox = document.forms[0].photos.value;
var hidden = document.forms[1].hphotos;

if(chkbox.checked = false){
hidden.value='0';
}
}
<input name="photos" type="checkbox" id="photos" class="input" onClick="changeoff();changeon();">

Thanks

fintan

SpaceFrog

4:08 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



if(chkbox.checked = true){
this assigns a value and is therefore always true

if(chkbox.checked == true){
or
if(chkbox.checked){

would be preferable

Longhaired Genius

4:53 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



I don't get it. If there is a checkbox associated with the value, how can it be considered hidden?

fintan

4:57 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Tried that, changeon works but changeoff doesn't seem to work.

Right I tried this and it works in firefox/opera but not in ie. Any ideas?

function changeoff(){
var chkbox = document.forms[0].photos;
var hidden = document.forms[1].hphotos;

if(chkbox.checked == chkbox.defaultChecked){
hidden.value='0';
}
}

function changeon(){
var chkbox = document.forms[0].photos;
var hidden = document.forms[1].hphotos;

if(chkbox.checked == true){
hidden.value='1';
}
}

Sorry the checkbox changes the value of a hidden field. So when it's ticked the hidden field equals 1 else it equals 0.

fintan

5:24 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Got it to work thanks.