|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Checking Boolean in Javascript
I have a very strange problem. I have a JavaScript function that gets a boolean value and checks or unchecks a checkbox based on the boolean value.
function initialize_p(thevalue) { if (thevalue) { document.form.cbx1.checked=true; } else { document.form.cbx1.checked=false; } In the example, I want form.cbx1 to be checked if 'thevalue' is true and unchecked if 'thevalue' is false. However, it's not working as desired. I used the alert stmt to see what it does: - When 'thevalue' is false, it goes to the 1st part of the if stmt and checks the checkbox - When 'thevalue' is true, it still goes to the 1st part of the if stmt and checks the checkbox. It looks it never goes to the else!! There have also been cases when it only goes to the else and not the if. I don't understand what's going on. Can anybody see what is wrong with the function? |
|
#2
|
||||
|
||||
|
Hi
Why do you need to use boolean? Can you not just do a string comparison? This would work equally well: Code:
<html>
<head>
<title>Check 'n' uncheck</title>
<script language="JavaScript">
function initialize_p(thevalue) {
if (thevalue == 'true') {
document.form.cbx1.checked=true;
} else {
document.form.cbx1.checked=false;
}
}
</script>
</head>
<body>
<form name="form">
<input type="checkbox" name="cbx1">
</form>
<a href="javascript:initialize_p('true');">Check it</a> | <a href="javascript:initialize_p('false');">Uncheck it</a>
</body>
</html>
|
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Checking Boolean in Javascript |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|