Click to See Complete Forum and Search --> : Checkbox and Javascript Message


doc3
04-11-2003, 01:10 PM
I have a form with a checkbox. What I would like to happen is that when the user checks or unchecks the checkbox I would like a message to appear beneath it depending on the state of the checkbox. For example. if checked: "hello"; if unchecked: "goodbye".

I believe this would be done through document.write with an if/else statement.

I would like the text to change without having to refresh the page as well as without the use of text fields. Therefore the message would look identical to the surrounding text.

I would be very grateful if anyone could help me out!

Thank you very much! :)

Dominic Cort

SeGamysa
04-11-2003, 01:28 PM
<script language = "javascript">

function check()
{
//checks to see which check boox is checked
var checkbox1 = -1;
for(var i = 0; i< document.FormName.CheckBoxName.length; i++)
{

if(document.FormName.CheckBoxName[i].checked)
{
checkbox1 = i;
}
}
if(checkbox1== "-1")
{
// then do this
}
else
{
// else do that
}
}
</script>

<form name = "FormName" action="post">
<input type = "checkbox" name="CheckBoxName" onClick="check()">
<form>


you mean something like this?
I added some stuff I figure ur ganna have multiple check boxes so I threw in the loop for later refrences

doc3
04-11-2003, 02:07 PM
thank you very much! almost there!

I will only be using one checkbox, but I'm sure your script will work just the same.

Is it possible for the message to come up in a set position? as at the moment it replaces the actual checkbox. I would like to keep the checkbox, and then on the line below it, insert the line of text. The line of text would be different depending on whether it is checked or not. my line of text will bealong the lines of, "you will be added to the database", or "you will not be added to the database".

---------------------- Example -------------------------------

Checkbox [ ] (unchecked)
you will not be added to the database

Checkbox (checked)
you will be added to the database

-----------------------------------------------------------------


Is this easy to do?

Thank you again!