checkbox doesn´t work

L

led

I have this function that changes textbox when checkbox is checked. But
can´t unchecked again.

this is the code:

<input type="checkbox" name="C1" value="1" onclick=altera_txt("C1","prec1")
<script type="text/javascript">

function altera_txt(ck,txt)
{
if (document.forms["fp1"].elements[ck].checked=false)
{
document.forms["fp1"].elements[txt].disabled=true
document.forms["fp1"].elements[txt].value=""
}
else
{
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
}
}
</script>
 
J

Jon Spivey

Hi,
In javascript you use 2 equal signs == to find out if a value is true or
false, eg
if(document.forms["fp1"].elements[ck].checked==false)
make this change and all will be well.

You could also use
if (!document.forms["fp1"].elements[ck].checked)
which would accomplish the same thing

Jon


led said:
I have this function that changes textbox when checkbox is checked. But
can´t unchecked again.

this is the code:

<input type="checkbox" name="C1" value="1"
onclick=altera_txt("C1","prec1")
<script type="text/javascript">

function altera_txt(ck,txt)
{
if (document.forms["fp1"].elements[ck].checked=false)
{
document.forms["fp1"].elements[txt].disabled=true
document.forms["fp1"].elements[txt].value=""
}
else
{
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
}
}
</script>
 
D

David Berry

You can't uncheck it again because your script says that once it's checked
the box is disabled, therefore you can't click in it anymore and fire off
another script to re-enable it.


led said:
I have this function that changes textbox when checkbox is checked. But
can´t unchecked again.

this is the code:

<input type="checkbox" name="C1" value="1"
onclick=altera_txt("C1","prec1")
<script type="text/javascript">

function altera_txt(ck,txt)
{
if (document.forms["fp1"].elements[ck].checked=false)
{
document.forms["fp1"].elements[txt].disabled=true
document.forms["fp1"].elements[txt].value=""
}
else
{
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
}
}
</script>
 
J

Jon Spivey

He's disabling the text box by checking the checked/unchecked status of the
checkbox. He's not trying to disable the checkbox. See my post - the error
is using 1 rather than 2 = signs to test if the checkbox is checked. With
that fixed it works fine.
if (document.forms["fp1"].elements[ck].checked=false)
will uncheck the checkbox and evaluate to true because it's been successful
so these lines will always run
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
whereas
if (document.forms["fp1"].elements[ck].checked==false)
will determine what to do based on the checked/unchecked status of the
checkbox - which is obviously what he wants.

I bet everyone that's ever written javascript has made that mistake :)

Jon


David Berry said:
You can't uncheck it again because your script says that once it's checked
the box is disabled, therefore you can't click in it anymore and fire off
another script to re-enable it.


led said:
I have this function that changes textbox when checkbox is checked. But
can´t unchecked again.

this is the code:

<input type="checkbox" name="C1" value="1"
onclick=altera_txt("C1","prec1")
<script type="text/javascript">

function altera_txt(ck,txt)
{
if (document.forms["fp1"].elements[ck].checked=false)
{
document.forms["fp1"].elements[txt].disabled=true
document.forms["fp1"].elements[txt].value=""
}
else
{
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
}
}
</script>
 
L

led

Obrigado (thanks in portuguese)

Jon Spivey said:
He's disabling the text box by checking the checked/unchecked status of
the checkbox. He's not trying to disable the checkbox. See my post - the
error is using 1 rather than 2 = signs to test if the checkbox is checked.
With that fixed it works fine.
if (document.forms["fp1"].elements[ck].checked=false)
will uncheck the checkbox and evaluate to true because it's been
successful so these lines will always run
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
whereas
if (document.forms["fp1"].elements[ck].checked==false)
will determine what to do based on the checked/unchecked status of the
checkbox - which is obviously what he wants.

I bet everyone that's ever written javascript has made that mistake :)

Jon


David Berry said:
You can't uncheck it again because your script says that once it's
checked the box is disabled, therefore you can't click in it anymore and
fire off another script to re-enable it.


led said:
I have this function that changes textbox when checkbox is checked. But
can´t unchecked again.

this is the code:

<input type="checkbox" name="C1" value="1"
onclick=altera_txt("C1","prec1")


<script type="text/javascript">

function altera_txt(ck,txt)
{
if (document.forms["fp1"].elements[ck].checked=false)
{
document.forms["fp1"].elements[txt].disabled=true
document.forms["fp1"].elements[txt].value=""
}
else
{
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
}
}
</script>
 
D

David Berry

I understand about the == problem. But what he said was "this function that
changes textbox when checkbox is checked. But can't unchecked again."

Unless I'm misunderstanding him, what he's saying is that once the box gets
checked and the JavaScript runs there's no way for him to go back and
uncheck the box.

In the script (after he fixes the == issue) it would say that if the box is
NOT Checked ( == false) then disable it and set the value to blank ("")
otherwise (if it IS Checked) then do not disable it and set the value to
"Preço"

In that case when he un-checks it then the code will disable the box. Once
that happens there's no way to go back and check the box again since it's
disabled.

In his code he has the JavaScript run on the onclick of the checkbox. I
*think* what he needs is to put the JavaScript on the OnSubmit of the form
to get the desired effect (disabling unchecked boxes and setting the value
of checked ones). Not sure if that's the intention though.



Jon Spivey said:
He's disabling the text box by checking the checked/unchecked status of
the checkbox. He's not trying to disable the checkbox. See my post - the
error is using 1 rather than 2 = signs to test if the checkbox is checked.
With that fixed it works fine.
if (document.forms["fp1"].elements[ck].checked=false)
will uncheck the checkbox and evaluate to true because it's been
successful so these lines will always run
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
whereas
if (document.forms["fp1"].elements[ck].checked==false)
will determine what to do based on the checked/unchecked status of the
checkbox - which is obviously what he wants.

I bet everyone that's ever written javascript has made that mistake :)

Jon


David Berry said:
You can't uncheck it again because your script says that once it's
checked the box is disabled, therefore you can't click in it anymore and
fire off another script to re-enable it.


led said:
I have this function that changes textbox when checkbox is checked. But
can´t unchecked again.

this is the code:

<input type="checkbox" name="C1" value="1"
onclick=altera_txt("C1","prec1")


<script type="text/javascript">

function altera_txt(ck,txt)
{
if (document.forms["fp1"].elements[ck].checked=false)
{
document.forms["fp1"].elements[txt].disabled=true
document.forms["fp1"].elements[txt].value=""
}
else
{
document.forms["fp1"].elements[txt].disabled=false
document.forms["fp1"].elements[txt].focus()
document.forms["fp1"].elements[txt].value="Preço"
}
}
</script>
 

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

Top