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>

Re: checkbox doesn´t work by Jon

Jon
Tue Nov 07 13:28:04 CST 2006

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" <tabemta@tabemta.com> wrote in message
news:eqTubBqAHHA.4024@TK2MSFTNGP04.phx.gbl...
>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>
>



Re: checkbox doesn´t work by David

David
Tue Nov 07 13:48:31 CST 2006

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" <tabemta@tabemta.com> wrote in message
news:eqTubBqAHHA.4024@TK2MSFTNGP04.phx.gbl...
>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>
>



Re: checkbox doesn´t work by Jon

Jon
Tue Nov 07 14:20:55 CST 2006

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" <dberry@mvps.org> wrote in message
news:e0q$0WqAHHA.3316@TK2MSFTNGP02.phx.gbl...
> 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" <tabemta@tabemta.com> wrote in message
> news:eqTubBqAHHA.4024@TK2MSFTNGP04.phx.gbl...
>>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>
>>
>
>



Re: checkbox doesn´t work by led

led
Tue Nov 07 15:23:46 CST 2006

Obrigado (thanks in portuguese)

"Jon Spivey" <jon@NotThis-Nisusnewmedia.com> escreveu na mensagem
news:OwVL9oqAHHA.5068@TK2MSFTNGP02.phx.gbl...
> 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" <dberry@mvps.org> wrote in message
> news:e0q$0WqAHHA.3316@TK2MSFTNGP02.phx.gbl...
>> 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" <tabemta@tabemta.com> wrote in message
>> news:eqTubBqAHHA.4024@TK2MSFTNGP04.phx.gbl...
>>>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>
>>>
>>
>>
>
>



Re: checkbox doesn´t work by David

David
Tue Nov 07 15:27:25 CST 2006

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" <jon@NotThis-Nisusnewmedia.com> wrote in message
news:OwVL9oqAHHA.5068@TK2MSFTNGP02.phx.gbl...
> 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" <dberry@mvps.org> wrote in message
> news:e0q$0WqAHHA.3316@TK2MSFTNGP02.phx.gbl...
>> 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" <tabemta@tabemta.com> wrote in message
>> news:eqTubBqAHHA.4024@TK2MSFTNGP04.phx.gbl...
>>>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>
>>>
>>
>>
>
>