HTML, JavaScript And CSS Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingHTML, JavaScript And CSS Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old September 1st, 2004, 11:43 PM
sn2003rk sn2003rk is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 9 sn2003rk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old September 6th, 2004, 04:46 AM
selwonk's Avatar
selwonk selwonk is offline
Contributing User
ASP Free Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2004
Posts: 2,942 selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 9 h 49 m 28 sec
Reputation Power: 62
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>
MK

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingHTML, JavaScript And CSS Help > Checking Boolean in Javascript


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT