Click to See Complete Forum and Search --> : checkbox and alert


rjusa
11-11-2003, 08:39 AM
I don't know why I'm having such a hard time with this. I have a checkbox (name=copy) that when checked I want an alert to be displayed. The code I tried is:

function SetFKolor() {

if (document.forms[0].copy.checked="true")
alert("the current value of checkbox is checked");
}


<input type="checkbox" name="copy" onmouseup="SetFColor">

Please help stupid.

Thanks,
Ron

fredmv
11-11-2003, 08:54 AM
I'll explain what you did wrong and how to correct everything. On the following line:if (document.forms[0].copy. Notice you're trying to reference document.forms[0], which is the first form in the document. You don't have an opening <form> tag before you define the checkbox, and therefore there is no first form that exists in the document. Without a pointer to that form, you won't be able to access values this way (unless you use DOM methods such as getElementById and the like). There is another error on the same line. In the following part of the line:checked="true")There are two incredibly big mistakes in this piece of code. First off, you're using the assignment operator (one equals sign) instead of the comparison operator. It's not trying to set the value of a null object to the string value true. If you want to check if the checkbox is checked, you shouldn't be quoting true since it is a boolean keyword in JavaScript. The final error doesn't exist within your JavaScript, but within your HTML code. In this piece of code: <input type="checkbox" name="copy" onmouseup="SetFColor"> See how you're calling the function SetFColor? Well, there's no function by that name in your JavaScript source code. It's actually called SetFKolor. This will return something along the lines of an object expected error. You also didn't include the two parentheses which defines it as a JavaScript function. So even if you had the right name for the function, it still wouldn't be called.Here's your source code with all of the errors listed above corrcted. The JavaScript:<script type="text/javascript">
function SetFKolor()
{
if (document.forms[0].copy.checked == true) alert("the current value of checkbox is checked");
}
</script>The HTML:<form>
<input type="checkbox" name="copy" onmouseup="SetFKolor();">
</form>I hope that helps you out.

Javium
11-11-2003, 10:23 AM
Ron, what about this:

<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language="JavaScript">
function SetFColor()
{
if (document.forms[0].copy.checked == true)
{
alert("the current value of checkbox is checked");
}
else
{
alert("NOT checked");
}
}
</script>
</head>
<body>
<form>
<input type="checkbox" name="copy">To be copied<br>
<input type="button" value=verify onClick="SetFColor();">
</form>
</body>
</html>

fredmv
11-11-2003, 10:31 AM
<input type="checkbox" onclick="alert((this.checked)?'It is checked!':'It is NOT checked!');" />

Javium
11-11-2003, 11:43 AM
Very nice Fred, but it's a different approach. You makes a direct confirmation, while I'm verifying when the user has finished setting up the checkbox (assuming there were several checkboxes). My function works more like the OK button in a standard program.
Anyway, your source code is very interesting! :cool: :)