check box

S

Samir

Here is my function

<script language="javascript">
document.form.B1.disabled = true;

function changebutton(){
if (document.form.C1.value = "ON"){
document.form.B1.disabled = false;
return true;
}
else {
document.form.B1.disabled = true;
return true;
}
}
</script>

So far it's doing what I want it to do, when the document loads it disables
the button, and when the check value is clicked it would enable the button.
Now here is the problem, when I uncheck the checkbox the button is not
disabled.

I am sure I am missing something very easy.
 
L

Lasse Reichstein Nielsen

Samir said:
Here is my function

<script language="javascript">
---
<script type="text/javascript">
---
The "type" attribute is required in all HTML versions that include the
script tag.
document.form.B1.disabled = true;

I recommend using:
---
document.forms['form'].elements['B1'].disabled = true;
---
if (document.form.C1.value = "ON"){

Should be "==", not "=". Using "=" makes it an assignment, and it always
evaluates to something true.
document.form.B1.disabled = false;
return true;
}
else {
document.form.B1.disabled = true;
return true;
}
}

This can be written shorter:
---
function changebutton(){
var elems = document.forms['form'].elements;
elems['B1'].disabled = (elems['C1'].value != "ON");
return true;
}
---
I am sure I am missing something very easy.

Yep. It's a classic (read: we have all done it at some point :)

/L
 
S

Samir

Still doesn't work. The same thing happens.

Lasse Reichstein Nielsen said:
Samir said:
Here is my function

<script language="javascript">
---
<script type="text/javascript">
---
The "type" attribute is required in all HTML versions that include the
script tag.
document.form.B1.disabled = true;

I recommend using:
---
document.forms['form'].elements['B1'].disabled = true;
---
if (document.form.C1.value = "ON"){

Should be "==", not "=". Using "=" makes it an assignment, and it always
evaluates to something true.
document.form.B1.disabled = false;
return true;
}
else {
document.form.B1.disabled = true;
return true;
}
}

This can be written shorter:
---
function changebutton(){
var elems = document.forms['form'].elements;
elems['B1'].disabled = (elems['C1'].value != "ON");
return true;
}
---
I am sure I am missing something very easy.

Yep. It's a classic (read: we have all done it at some point :)

/L
 
K

kaeli

Here is my function

If it works, it works in IE only.
<script language="javascript">

document.form.B1.disabled = true;
If you named your form 'form', it's not really recommended. Name it
something else, like form1, then do the following for cross-browser
syntax.
document.forms["form1"].elements["B1"].disabled = true;

Do the same for the rest of the references you make to it.
function changebutton(){
if (document.form.C1.value = "ON"){

double-equals for equality check.
if (document.forms["form1"].elements["C1"].value == "ON")
So far it's doing what I want it to do, when the document loads it disables
the button, and when the check value is clicked it would enable the button.
Now here is the problem, when I uncheck the checkbox the button is not
disabled.

That equals things is probably why.


--
 
S

Samir

Still doesn't work.


kaeli said:
Here is my function

If it works, it works in IE only.
<script language="javascript">

document.form.B1.disabled = true;
If you named your form 'form', it's not really recommended. Name it
something else, like form1, then do the following for cross-browser
syntax.
document.forms["form1"].elements["B1"].disabled = true;

Do the same for the rest of the references you make to it.
function changebutton(){
if (document.form.C1.value = "ON"){

double-equals for equality check.
if (document.forms["form1"].elements["C1"].value == "ON")
So far it's doing what I want it to do, when the document loads it disables
the button, and when the check value is clicked it would enable the button.
Now here is the problem, when I uncheck the checkbox the button is not
disabled.

That equals things is probably why.


--
--
~kaeli~
Shotgun wedding: A case of wife or death.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
K

kaeli

Still doesn't work.

Post the FULL code you're using or URL. My guess is that the value is
never equal to "ON" or some such, but we can't tell by only looking at
the function what values you're using in your HTML.

--
--
~kaeli~
If a chicken and a half can lay an egg and a half in a day
and a half, how long would it take for a monkey with a
wooden leg to kick the dill seeds out of a pickle?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
M

Mick White

Samir said:
Here is my function

<script language="javascript">
document.form.B1.disabled = true;

Here's mine:
function changebutton(){
document.form.B1.disabled=!document.form.C1.checked;
}
if (document.form.C1.value = "ON"){

C1.value always = "ON", its "checked" property changes. That's what you
need to monitor.
document.form.B1.disabled = false;
return true;
Why are you returning a Boolean?

This "else" statement never runs
document.form.B1.disabled = true;
return true;
}
}
</script>
I am sure I am missing something very easy.


Mick
 
S

Samir

Never mind this seems to work for checking

if (document.forms["form1"].elements["C1"].checked == true)

Thanks again
 
L

Lasse Reichstein Nielsen

Samir said:
Never mind this seems to work for checking

if (document.forms["form1"].elements["C1"].checked == true)

You don't need to compare to true. Just do:
if (document.forms["form1"].elements["C1"].checked)
It is already a boolean.
/L
 
S

Samir

Okay...thanks, that works just like you said.

Lasse Reichstein Nielsen said:
Samir said:
Never mind this seems to work for checking

if (document.forms["form1"].elements["C1"].checked == true)

You don't need to compare to true. Just do:
if (document.forms["form1"].elements["C1"].checked)
It is already a boolean.
/L
 
M

Mick White

Samir said:
Never mind this seems to work for checking

if (document.forms["form1"].elements["C1"].checked == true)

Thanks again
Did you not see my response?

function changebutton(){
document.form.B1.disabled=!document.form.C1.checked;
}
Mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,301
Latest member
BuyPureganics

Latest Threads

Top