Testing a checkbox

T

Trevor L.

I have this code

<form action="" id="form1" name="form1">
Please answer yes or no <br/>
<input type="checkbox" id="yes" name="yes" />Yes <br/>
<input type="checkbox" id="no" name="no" />No <br/>
<input type="button" value="Send" onclick="sendmess()" />
</form>

This results in this being displayed
Please answer yes or no
Yes
No

No matter whether I tick or don't tick either box, the value returned is on.
e.g. I have a function:
function sendmess()
{
alert ('YES ' + document.forms['form1'].elements['yes'].value +
' NO ' + document.forms['form1'].elements['no'].value )
return
}

On clicking Send, all of these return the same result
Yes
No

Yes
No

Yes
No

Yes
No


Yes
No

The result is an alert:
YES no NO on

How do I test which box has been ticked ?
 
T

Thomas A. Rowe

You haven't added the Value attribute.

<form action="" id="form1" name="form1">
Please answer yes or no <br/>
<input type="checkbox" id="yes" name="yes" value="Yes" />Yes <br/>
<input type="checkbox" id="no" name="no" value="No" />No <br/>
<input type="button" value="Send" onclick="sendmess()" />
</form>


--
==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
==============================================
If you feel your current issue is a results of installing
a Service Pack or security update, please contact
Microsoft Product Support Services:
http://support.microsoft.com
If the problem can be shown to have been caused by a
security update, then there is usually no charge for the call.
==============================================
 
S

Stefan B Rusynko

See http://www.irt.org/articles/js027/index.htm

--

_____________________________________________
SBR @ ENJOY (-: [ Microsoft MVP - FrontPage ]
"Warning - Using the F1 Key will not break anything!" (-;
To find the best Newsgroup for FrontPage support see:
http://www.frontpagemvps.com/FrontPageNewsGroups/tabid/53/Default.aspx
_____________________________________________


|I have this code
|
| <form action="" id="form1" name="form1">
| Please answer yes or no <br/>
| <input type="checkbox" id="yes" name="yes" />Yes <br/>
| <input type="checkbox" id="no" name="no" />No <br/>
| <input type="button" value="Send" onclick="sendmess()" />
| </form>
|
| This results in this being displayed
| Please answer yes or no
| Yes
| No
|
| No matter whether I tick or don't tick either box, the value returned is on.
| e.g. I have a function:
| function sendmess()
| {
| alert ('YES ' + document.forms['form1'].elements['yes'].value +
| ' NO ' + document.forms['form1'].elements['no'].value )
| return
| }
|
| On clicking Send, all of these return the same result
| Yes
| No
|
| Yes
| No
|
| Yes
| No
|
| Yes
| No
|
|
| Yes
| No
|
| The result is an alert:
| YES no NO on
|
| How do I test which box has been ticked ?
|
| --
| Cheers,
| Trevor L.
| Website: http://tandcl.homemail.com.au
|
|
 
T

Trevor L.

Thomas,

This makes no difference whatsoever

I changed it to
<input type="checkbox" id="yes" name="yes" value="Yes"/>Yes <br/>
<input type="checkbox" id="no" name="no" value="No" />No <br/>

but no matter which checkbox is checked or not, using this code to display
the result
document.forms['form1'].elements['yes'].value
document.forms['form1'].elements['no'].value
gets the result
Yes
No

Is the problem with the Javascript ?
 
T

Thomas A. Rowe

The problem is with your JavaScript. Are you also using FP Form Field validation, if so that would
be a reason for it not working.

--
==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
==============================================
If you feel your current issue is a results of installing
a Service Pack or security update, please contact
Microsoft Product Support Services:
http://support.microsoft.com
If the problem can be shown to have been caused by a
security update, then there is usually no charge for the call.
==============================================

Trevor L. said:
Thomas,

This makes no difference whatsoever

I changed it to
<input type="checkbox" id="yes" name="yes" value="Yes"/>Yes <br/>
<input type="checkbox" id="no" name="no" value="No" />No <br/>

but no matter which checkbox is checked or not, using this code to display the result
document.forms['form1'].elements['yes'].value
document.forms['form1'].elements['no'].value
gets the result
Yes
No

Is the problem with the Javascript ?

--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au
You haven't added the Value attribute.

<form action="" id="form1" name="form1">
Please answer yes or no <br/>
<input type="checkbox" id="yes" name="yes" value="Yes" />Yes <br/>
<input type="checkbox" id="no" name="no" value="No" />No <br/>
<input type="button" value="Send" onclick="sendmess()" />
</form>
 
S

Stefan B Rusynko

you're welcome

--

_____________________________________________
SBR @ ENJOY (-: [ Microsoft MVP - FrontPage ]
"Warning - Using the F1 Key will not break anything!" (-;
To find the best Newsgroup for FrontPage support see:
http://www.frontpagemvps.com/FrontPageNewsGroups/tabid/53/Default.aspx
_____________________________________________


| Stefan B Rusynko wrote:
| > See http://www.irt.org/articles/js027/index.htm
|
| Stefan,
|
| I forgot to say thanks. As stated in the reference, the correct property to
| test is "checked". When I did this all was fine.
| --
| Cheers,
| Trevor L.
| Website: http://tandcl.homemail.com.au
|
|
 

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