| | |
how can I keep "checked" boxes within div to default to "unchecked" if div is"hidden"
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 63
Reputation:
Solved Threads: 0
how can I keep "checked" boxes within div to default to "unchecked" if div is"hidden"
0
#1 Jun 18th, 2009
Hi, all:
I got a form where some checbox fields will hide or show as user clicks specific choices. Problem is, if one mistakenly checks one of these checkboxes and then hides it, it still does retain the checked value, even if hidden. How can I make sure that the checkbox defaults back to "unchecked" if in fact is "hidden"??
Here's my javascript code:
and here is my basic form-html code:
I got a form where some checbox fields will hide or show as user clicks specific choices. Problem is, if one mistakenly checks one of these checkboxes and then hides it, it still does retain the checked value, even if hidden. How can I make sure that the checkbox defaults back to "unchecked" if in fact is "hidden"??
Here's my javascript code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> <!-- function toggle_visibility(id) { var e = document.getElementById(id); if(e.style.display == 'none') e.style.display = 'block'; else e.style.display = 'none'; } //--> </script> <script type="text/javascript"> <!-- function toggle_visibilityNone(id) { var e = document.getElementById(id); if(e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'none'; } //--> </script> <script type="text/javascript"> <!-- function toggle_visibilityYes(id) { var e = document.getElementById(id); if(e.style.display == 'none') e.style.display = 'block'; else e.style.display = 'block'; } //--> </script>
and here is my basic form-html code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<form action="<?php echo $editFormAction; ?>" id="insertForm" name="insertForm" method="POST"> <p> </p> <table width="375" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td colspan="3" align="center"><img src="../images/nc_title_logo.gif" width="342" height="54" /></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td> </td> <td colspan="2" align="center"> </td> </tr> <tr> <td width="314"><span class="style5">No Order completed in this call </span></td> <td width="61" colspan="2" align="center"> <input name="noorder" type="checkbox" id="noorder" value="checkbox" onclick="toggle_visibility('box');" /> </td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td colspan="3"><div id="box"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="100%"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr class="color-table"> <td > </td> <td align="left"> </td> <td align="left"> </td> </tr> <tr class="color-table"> <td > </td> <td align="left"><strong>YES</strong></td> <td align="left"><strong>NO</strong></td> </tr> <tr> <td width="80%" class="color-table"> 1) Did you attempt an Upsell?</td> <td class="color-table"><input type="radio" name="upsell" value="Y" onclick="toggle_visibilityYes('upsellBox');"/></td> <td align="center" class="color-table"><input type="radio" name="upsell" value="N" onclick="toggle_visibilityNone('upsellBox');"/></td> </tr> </table></td> </tr> <tr> <td class="color-table"> </td> </tr> <tr> <td class="color-table"><div id="upsellBox"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="80%" class="color-table"> 2) Was the Upsell successful? </td> <td class="color-table"><input type="radio" name="upsellStatus" value="Y" /></td> <td align="center" class="color-table"><input type="radio" name="upsellStatus" value="N" /></td> </tr> <tr> <td class="color-table"> </td> <td class="color-table"> </td> <td align="center" class="color-table"> </td> </tr> </table> </div></td> </tr> <tr> <td class="color-table-2"> </td> </tr> <tr> <td class="color-table-2"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="color-table-2"> </td> <td align="left" class="color-table-2"><strong>YES</strong></td> <td colspan="2" align="left" class="color-table-2"><strong>NO</strong></td> </tr> <tr> <td width="80%" class="color-table-2"> 3) Did you attempt a Cross-Sell? </td> <td class="color-table-2"><input type="radio" name="cross" value="Y" onclick="toggle_visibilityYes('crossBox');"/></td> <td colspan="2" align="center" class="color-table-2"><input type="radio" name="cross" value="N" onclick="toggle_visibilityNone('crossBox');"/></td> </tr> </table></td> </tr> <tr> <td class="color-table-2"> </td> </tr> <tr> <td><div id="crossBox"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="80%" class="color-table-2"> 4) Was the Cross-Sell successful? </td> <td class="color-table-2"><input type="radio" name="crossStatus" value="Y" /></td> <td align="center" class="color-table-2"><input type="radio" name="crossStatus" value="N" /></td> </tr> <tr> <td class="color-table-2"> </td> <td class="color-table-2"> </td> <td align="center" class="color-table-2"> </td> </tr> </table> </div></td> </tr> </table> </div> </td> </tr> <tr> <td> </td> <td colspan="2" align="center"> </td> </tr> <tr> <td><input name="agentid" type="hidden" id="agentid" value="<?php echo $_GET['agentid']; ?>" /> <input name="first" type="hidden" id="first" value="<?php echo $_GET['first']; ?>" /> <input name="last" type="hidden" id="last" value="<?php echo $_GET['last']; ?>" /> <input name="startdate" type="hidden" id="startdate" value="<?php $newdate = $_GET['startdate']; $newdateMonth = substr($newdate,0,2); $newdateDay = substr($newdate,2,2); $newdateYear = substr($newdate,4,4); $newFinalDate = $newdateYear . "-" . $newdateMonth . "-" . $newdateDay; echo $newFinalDate; ?>" /> <input name="inqueue" type="hidden" id="inqueue" value="<?php echo $_GET['inqueue']; ?>" /> <input name="skill" type="hidden" id="skill" value="<?php echo $_GET['skill']; ?>" /> <input name="skillname" type="hidden" id="skillname" value="<?php echo $_GET['skillname']; ?>" /> <input name="contactid" type="hidden" id="contactid" value="<?php echo $_GET['contactid']; ?>" /> <input name="masterid" type="hidden" id="masterid" value="<?php echo $_GET['masterid']; ?>" /> <input name="dnis" type="hidden" id="dnis" value="<?php echo $_GET['dnis']; ?>" /> <input name="ani" type="hidden" id="ani" value="<?php echo $_GET['ani']; ?>" /> </td> <td colspan="2" align="center"><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> <input type="hidden" name="MM_insert" value="insertForm"> </form>
Re: how can I keep "checked" boxes within div to default to "unchecked" if div is"hidden"
1
#2 Jun 18th, 2009
Surfer,
First of all, you can massively simplify your form by purging all the nested tables.
Notes:
You may need to readdress classes. I may have over-simplified things by moving all class definitions from <td>s to <tbody> tags.
Anyways, I hope this helps.
Airchow
First of all, you can massively simplify your form by purging all the nested tables.
html Syntax (Toggle Plain Text)
<form action="<?php echo $editFormAction; ?> " id="insertForm" name="insertForm" method="POST"> <table width="375" border="0" align="center" cellpadding="0" cellspacing="0"> <tbody> <tr> <td colspan="3" align="center"> <img src="../images/nc_title_logo.gif" width="342" height="54" /> </td> </tr> <tr height="35" valign="bottom"> <td width="80%"> <span class="style5"> No Order completed in this call</span> </td> <td width="10%" align="center"> <input name="noorder" type="checkbox" id="noorder" value="checkbox" onclick="toggle_visibility(['upsell', 'upsellBox', 'cross', 'crossBox'], 0, ['upsell', 'upsellStatus', 'cross', 'crossStatus']);" /> </td> <td width="10%"> </td> </tr> </tbody> <tbody id="upsell" class="color-table"> <tr height="35" valign="bottom"> <td> </td> <td align="center"> <strong> YES</strong> </td> <td align="center"> <strong> NO</strong> </td> </tr> <tr> <td> 1) Did you attempt an Upsell?</td> <td align="center"> <input type="radio" name="upsell" value="Y" onclick="toggle_visibility('upsellBox', 1, 'upsellStatus');"/> </td> <td align="center"> <input type="radio" name="upsell" value="N" onclick="toggle_visibility('upsellBox', -1, 'upsellStatus');"/> </td> </tr> </tbody> <tbody id="upsellBox" class="color-table"> <tr> <td> 2) Was the Upsell successful? </td> <td align="center"> <input type="radio" name="upsellStatus" value="Y" /> </td> <td align="center"> <input type="radio" name="upsellStatus" value="N" /> </td> </tr> </tbody> <tbody id="cross" class="color-table-2"> <tr height="35" valign="bottom"> <td> </td> <td align="center"> <strong> YES</strong> </td> <td align="center"> <strong> NO</strong> </td> </tr> <tr> <td> 3) Did you attempt a Cross-Sell? </td> <td align="center"> <input type="radio" name="cross" value="Y" onclick="toggle_visibility('crossBox', 1, 'crossStatus');"/> </td> <td align="center"> <input type="radio" name="cross" value="N" onclick="toggle_visibility('crossBox', -1, 'crossStatus');"/> </td> </tr> </tbody> <tbody id="crossBox" class="color-table-2"> <tr> <td> 4) Was the Cross-Sell successful? </td> <td align="center"> <input type="radio" name="crossStatus" value="Y" /> </td> <td align="center"> <input type="radio" name="crossStatus" value="N" /> </td> </tr> </tbody> <tbody> <tr height="35" valign="bottom"> <td> </td> <td colspan="2" align="center"> <input type="submit" name="Submit" value="Submit" /> </td> </tr> </tbody> </table> <input type="hidden" name="MM_insert" value="insertForm"> <input name="agentid" type="hidden" id="agentid" value="<?php echo $_GET['agentid']; ?>" /> <input name="first" type="hidden" id="first" value="<?php echo $_GET['first']; ?> " /> <input name="last" type="hidden" id="last" value="<?php echo $_GET['last']; ?>" /> <input name="startdate" type="hidden" id="startdate" value="<?php $newdate = $_GET['startdate']; $newdateMonth = substr($newdate,0,2); $newdateDay = substr($newdate,2,2); $newdateYear = substr($newdate,4,4); $newFinalDate = $newdateYear . "-" . $newdateMonth . "-" . $newdateDay; echo $newFinalDate; ?> " /> <input type="hidden" name="inqueue" id="inqueue" value="<?php echo $_GET['inqueue']; ?>" /> <input type="hidden" name="skill" id="skill" value="<?php echo $_GET['skill']; ?> " /> <input type="hidden" name="skillname" id="skillname" value="<?php echo $_GET['skillname']; ?>" /> <input type="hidden" name="contactid" id="contactid" value="<?php echo $_GET['contactid']; ?> " /> <input type="hidden" name="masterid" id="masterid" value="<?php echo $_GET['masterid']; ?>" /> <input type="hidden" name="dnis" id="dnis" value="<?php echo $_GET['dnis']; ?> " /> <input type="hidden" name="ani" id="ani" value="<?php echo $_GET['ani']; ?>" /> </form>
- <tbody id="..."> is used to make addressable portions of a single table.
- Judicious use of <tr height=".." valign="bottom"> obviates the need for spacing rows in the table.
javascript Syntax (Toggle Plain Text)
function toggle_visibility(idArray, force, radioNamesArray) { force = (!force) ? null : (force <= -1) ? 'none' : 'block'; if(typeof idArray == 'string'){ idArray = [idArray]; }//if string, then convert to array if(radioNamesArray){ uncheck(radioNamesArray); } for(var i=0; i<idArray.length; i++) { var e = (document.getElementById) ? document.getElementById(idArray[i]) : document.all[idArray[i]]; if(force){ e.style.display = force; } else{ e.style.display = (e.style.display == 'none') ? 'block' : 'none'; } } } function uncheck(radioNamesArray) { if(typeof radioNamesArray == 'string'){ radioNamesArray = [radioNamesArray]; }//if string, then convert to array var elements = document.getElementsByTagName('input'); for(var i=0; i<elements.length; i++) { for(var j=0; j<radioNamesArray.length; j++) { if(elements[i].name && elements[i].name == radioNamesArray[j]) { elements[i].checked = 0; } } } }
Anyways, I hope this helps.
Airchow
Last edited by Airshow; Jun 18th, 2009 at 9:12 pm.
50% of the solution lies in accurately describing the problem!
•
•
Join Date: Mar 2007
Posts: 63
Reputation:
Solved Threads: 0
Re: how can I keep "checked" boxes within div to default to "unchecked" if div is"hidden"
0
#3 Jun 19th, 2009
Hi, Airshow...
You are awesome! As you can see, I can barely grasp Javascript, so very much appreciate your help!
I did notice, it did work pretty smooth in Explorer, but things get all out of place if I try it in Firefox, like table gets all mumbled-jumbled.. and was wansdering if there is something that can be modified, so it also looks good there? I think, possibly as you say the over-simplification of the td vs. tbody tags? but I just dont know exactly what to modify...
Thanks, very much appreciate it!!
You are awesome! As you can see, I can barely grasp Javascript, so very much appreciate your help!
I did notice, it did work pretty smooth in Explorer, but things get all out of place if I try it in Firefox, like table gets all mumbled-jumbled.. and was wansdering if there is something that can be modified, so it also looks good there? I think, possibly as you say the over-simplification of the td vs. tbody tags? but I just dont know exactly what to modify...
Thanks, very much appreciate it!!
Last edited by websurfer; Jun 19th, 2009 at 11:17 am.
Re: how can I keep "checked" boxes within div to default to "unchecked" if div is"hidden"
0
#4 Jun 19th, 2009
Surfer,
By crickey, you're right about Firefox behaving oddly. I tracked it down to the style directive for turning hidden <tbody> elements back on. IE takes 'block' while FF/opera etc. take 'table-row-group' !!! The latter is both standards compliant and unguessable.
So you need some new code. Replace the function toggle_visibility with :
I have changed the name to more correctly reflect the nature of the function.
You must also replace five lines in the HTML :
These lines have only changed in their detail, so it should be easy to find the five original lines.
My proviso about maybe having oversimplified the class definitions still stands. This won't affect the basic switching functionality, but may affect appearance (text/background color etc) in that your <tr>s and/or <td>s may rely on being individually styled (as per your original HTML) rather than having a "group style" applied at <tbody> level. I can't tell without seeing your stylesheet.
Anyway, for now see if the changes fix things in FF.
Airshow
By crickey, you're right about Firefox behaving oddly. I tracked it down to the style directive for turning hidden <tbody> elements back on. IE takes 'block' while FF/opera etc. take 'table-row-group' !!! The latter is both standards compliant and unguessable.
So you need some new code. Replace the function toggle_visibility with :
javascript Syntax (Toggle Plain Text)
function set_display(idArray, state, radioNamesArray) { var method = 'display'; state = (!state || state < 0) ? 0 : 1; if(typeof idArray == 'string'){ idArray = [idArray]; }//if string, then convert to array if(radioNamesArray){ uncheck(radioNamesArray); } for(var i=0; i<idArray.length; i++) { var el = (document.getElementById) ? document.getElementById(idArray[i]) : document.all[idArray[i]]; if(!el) { return; } try { el.style.display = (state) ? 'table-row-group' : 'none'; }//firefox, opera etc. catch(err) { try { el.style.display = (state) ? 'block' : 'none'; }//ie catch (err) { ; } } } }
You must also replace five lines in the HTML :
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<td width="10%" align="center"><input name="noorder" type="checkbox" id="noorder" value="checkbox" onclick="set_display(['upsell', 'upsellBox', 'cross', 'crossBox'], !this.checked, ['upsell', 'upsellStatus', 'cross', 'crossStatus']);" /></td> .... <td align="center"><input type="radio" name="upsell" value="Y" onclick="set_display('upsellBox', 1, 'upsellStatus');"/></td> <td align="center"><input type="radio" name="upsell" value="N" onclick="set_display('upsellBox', 0, 'upsellStatus');"/></td> .... <td align="center"><input type="radio" name="cross" value="Y" onclick="set_display('crossBox', 1, 'crossStatus');"/></td> <td align="center"><input type="radio" name="cross" value="N" onclick="set_display('crossBox', 0, 'crossStatus');"/></td>
My proviso about maybe having oversimplified the class definitions still stands. This won't affect the basic switching functionality, but may affect appearance (text/background color etc) in that your <tr>s and/or <td>s may rely on being individually styled (as per your original HTML) rather than having a "group style" applied at <tbody> level. I can't tell without seeing your stylesheet.
Anyway, for now see if the changes fix things in FF.
Airshow
50% of the solution lies in accurately describing the problem!
•
•
Join Date: Mar 2007
Posts: 63
Reputation:
Solved Threads: 0
Re: how can I keep "checked" boxes within div to default to "unchecked" if div is"hidden"
0
#5 Jun 20th, 2009
Hi again, Air:
YOU ARE DARN GOOD...! and you call yourself "Whiz in Training!"??? how about "Great Master"... your fixes worked right on!
Now it all works beautifully in all browsers, including my Mac!
Thanks so much for taking the time to look at my problem... and hanging in there for me. Very much appreciate it!
Surfer
YOU ARE DARN GOOD...! and you call yourself "Whiz in Training!"??? how about "Great Master"... your fixes worked right on!
Now it all works beautifully in all browsers, including my Mac!
Thanks so much for taking the time to look at my problem... and hanging in there for me. Very much appreciate it!
Surfer
![]() |
Similar Threads
- Flash: Arrows moving according to some function (Graphics and Multimedia)
- Send data of card number with four input field to mysql (PHP)
- 3-column-header in blog (HTML and CSS)
- Form's controls data takes too long to load (ASP)
- Can't create sessions (PHP)
- CSS - Making div vertically "overflow" (HTML and CSS)
- I get "[F1] for setup, [F2] to load default settings." And the Keyboard is dead. (Troubleshooting Dead Machines)
- document.write to new window and into a 3 column table! Help Please! (JavaScript / DHTML / AJAX)
- "C:\system32\bridge.dll module cannot be located" problem (Viruses, Spyware and other Nasties)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Sending the value of text field
- Next Thread: Ajax (?) dropdown menu pulling info from MYSQL database
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate array automatically beta box bug calendar captchaformproblem cart checkbox child class close codes column createrange() css cursor decimal dependent design disablefirebug dom download dropdown editor element engine enter error events explorer ext file firefox focus forms frameworks google gwt gxt html htmlform ie8 iframe images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jsp jump listbox masterpage math menu microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent pdf php player post problem progressbar prototype redirect regex runtime scale scroll search select shopping size sql text textarea toggle w3c web website window windowofwords windowsxp wysiwyg \n





