/    Sign up×
Community /Pin to ProfileBookmark

The code below is suppose to work like …

There are three checkboxes per item. Each item is incremented by number (the one shown below is for brevity sake). When a user checks one of the three boxes for an item, the javascript should fire and perform one of the following …

1) If nah is checked, make sure the done and todo are unchecked
2) If nah is unchecked and neither done or todo are checked, check todo
3) If todo or done is checked, make sure nah is not checked
4) Todo and done may both be checked

Here is my broken code. All help is appreciated!

[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>
<title>Test Form</title>
</head>

<body>

<script language=”text/javascript”>
function _Check_Boxes(_chk_type,_chk_bid){
alert(‘In function’);

var _todo = document.getElementById(‘todo_’ + _chk_bid);
var _done = document.getElementById(‘done_’ + _chk_bid);
var _nah = document.getElementById(‘nah_’ + _chk_bid);

if (_chk_type==’N’) {
if (_nah.checked==true) {
// If N box is checked, set other to to unchecked
_todo.checked = false;
_done.checked = false;
else
// If it isn’t checked, see if either of the others are.
// If not, check todo
if (_done.checked==false && _todo.checked==false) {
// Just set the todo box
_todo.checked==true;
}
}
else
// Something happened to the done_ or todo_ boxes
if (_done.checked==false && _todo.checked==false) {
// Just set the nah box if neither of the done or todo boxes are checked
_nah.checked==true;
}
}
return true;
}
</script>

<form name=”main_form”>
<table>
<tr>
<td style=’width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;’>Done</td>
<td style=’width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;’>To&nbsp;Do</td>
<td style=’width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;’>Nah!</td>
<td style=’font-size:1.6em;font-weight:bold;’>Item</td>
</tr>
<tr>
<td style=’text-align:center;padding-right:5px;’><input type=”checkbox” name=”done_1″ onClick=”_Check_Boxes(‘D’,1);” style=’font-size:1.5em;’ /></td>
<td style=’text-align:center;padding-right:5px;’><input type=”checkbox” name=”todo_1″ onClick=”_Check_Boxes(‘T’,1);” style=’font-size:1.5em;’ /></td>
<td style=’text-align:center;padding-right:5px;’><input type=”checkbox” name=”nah_1″ onClick=”_Check_Boxes(‘N’,1);” style=’font-size:1.5em;’ checked /></td>
<td style=’font-size:1.3em;’>Test Line</td>
</tr>
</table>
</form>

</body>

</html>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@kennyvAug 30.2009 — I changed a few things.

Instead of using getElementById it is much easier to pass the name of the form and use the element name instead.

I got rid of the IF statements and used instead the switch statement.

Should work OK for you.

Try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Test Form</title>

</head>

<body>

<script language="javascript">

function _Check_Boxes(tform,_chk_type,_chk_bid){

switch(_
chk_type)

{

case "D":

tform.todo_1.checked = false;

tform.nah_1.checked = false;

break;

case "T":

tform.done_1.checked = false;

tform.nah_1.checked = false;

break;

case "N":

tform.todo_1.checked = false;

tform.done_1.checked = false;

break;

}

}

</script>


<form name="main_form">

<table>

<tr>

<td style='width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;'>Done</td>

<td style='width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;'>To&nbsp;Do</td>

<td style='width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;'>Nah!</td>

<td style='font-size:1.6em;font-weight:bold;'>Item</td>

</tr>

<tr>

<td style='text-align:center;padding-right:5px;'><input type="checkbox" id="done_1" name="done_1" onClick="_Check_Boxes(this.form,'D',1);" style='font-size:1.5em;' /></td>

<td style='text-align:center;padding-right:5px;'><input type="checkbox" id="todo_1" name="todo_1" onClick="_
Check_Boxes(this.form,'T',1);" style='font-size:1.5em;' /></td>

<td style='text-align:center;padding-right:5px;'><input type="checkbox" id="nah_1" name="nah_1" onClick="_Check_Boxes(this.form,'N',1);" style='font-size:1.5em;' checked /></td>

<td style='font-size:1.3em;'>Test Line</td>


</tr>

</table>

</form>

</body>

</html>

Ken

www.webdesigntips-foreveryone.com
Copy linkTweet thisAlerts:
@lsutiger89authorAug 31.2009 — Ken,

Thanks for the response! One question though .... I am passing the number of the control which will be incremented ... 1, 2, 3, etc. Can I use that number instead of hard coding that? Maybe using the eval function ... slow but it may work???

Thanks,

Bill
Copy linkTweet thisAlerts:
@lsutiger89authorAug 31.2009 — And if so, how would you suggest doing it. If not, what do you feel best? Thanks!
Copy linkTweet thisAlerts:
@JMRKERAug 31.2009 — Question.

In the request, it was stated that 'ToDo' and 'Done' could both be checked.

But the solution of post #2 does not allow this condition.

If the solution is adequate, why not use radio buttons instead of the checkboxes

and eliminate a lot of the condition checking? ?

But, if you still want to do it on multiple checkboxes, then consider this modification of post #2:
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;title&gt;Test Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;script language="javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1031974#post1031974

function _Check_Boxes(tform,_chk_type,_chk_bid){
var todo = 'todo_'+_chk_bid;
var done = 'done_'+_chk_bid;
var nah = 'nah_'+_chk_bid;
switch(_chk_type) {
case "D":
document.getElementById(todo).checked = false;
document.getElementById(nah).checked = false;
break;
case "T":
document.getElementById(done).checked = false;
document.getElementById(nah).checked = false;
break;
case "N":
document.getElementById(todo).checked = false;
document.getElementById(done).checked = false;
break;
}
}
&lt;/script&gt;

&lt;form name="main_form"&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td style='width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;'&gt;Done&lt;/td&gt;
&lt;td style='width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;'&gt;To&amp;nbsp;Do&lt;/td&gt;
&lt;td style='width:60px;text-align:center;font-size:1.6em;font-weight:bold;padding-right:5px;'&gt;Nah!&lt;/td&gt;
&lt;td style='font-size:1.6em;font-weight:bold;'&gt;Item&lt;/td&gt;
&lt;/tr&gt;
&lt;script type="text/javascript"&gt;
var str = '';
for (var i=1; i&lt;16; i++) {
str += '&lt;tr&gt;';
str += "&lt;td style='text-align:center;padding-right:5px;'&gt;";
str += '&lt;input type="checkbox" id="done_'+i+'" name="done_'+i+'"';
str += ' onClick="_Check_Boxes(this.form,'D','+i+');" style='font-size:1.5em;' /&gt;&lt;/td&gt;';
str += "&lt;td style='text-align:center;padding-right:5px;'&gt;";
str += '&lt;input type="checkbox" id="todo_'+i+'" name="todo_'+i+'"';
str += ' onClick="_Check_Boxes(this.form,'T','+i+');" style='font-size:1.5em;' /&gt;&lt;/td&gt;';
str += "&lt;td style='text-align:center;padding-right:5px;'&gt;";
str += '&lt;input type="checkbox" id="nah_'+i+'" name="nah_'+i+'"';
str += ' onClick="_Check_Boxes(this.form,'N','+i+');" style='font-size:1.5em;' checked /&gt;&lt;/td&gt;';
str += "&lt;td style='font-size:1.3em;'&gt;Test Line: "+i+"&lt;/td&gt;";
str += '&lt;/tr&gt;';
}
document.write(str);
&lt;/script&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @lsutiger89 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...