| | |
Hide fields depending on selection
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 25
Reputation:
Solved Threads: 0
In the following code I am attempting to get certain fields to only appear if the user Selects dependent else those fields should be hidden.
Please tell me what I have done wrong?
Please tell me what I have done wrong?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!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" xml:lang="en" lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>Display and Hide</title> <script language="JavaScript"> function showhidefield() { if (document.HelpAdd.RecordType.value == "Parent") { document.getElementById("hideablearea").style.visibility = "hidden"; } else { document.getElementById("hideablearea").style.visibility = "visible"; } } </script> </head> <body> <form name='HelpAdd' action='nextpage.asp'> <table align="center" border="3" cellspacing="0" cellpadding="3"> <tr><td>Record Type:</td> <td><select id="RecordType" name="RecordType" onchange=showhidefield()> <option value='0'>Parent</option> <option value='1'>Dependant</option><br/> <tr><td>Topic Title:</td> <td><input type="text" name="TopicName" ID="TopicName" maxlength="25"></td></tr> <div id='hideablearea' style='visibility:hidden;'> <tr><td>Parent:</td> <td><select name="Parent"> <input type="text" name="ParentID" ID="ParentID" maxlength="25"></td></tr> <tr><td>Topic Body:</td> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $MaxFileSize ?>" /> <td><input id="helpfile" name="helpfile" type="file" /></td></tr> </div> <tr><td colspan="2" align="center"> <input type="submit" name="submit" value="Submit"> <a href="/help.php"><button>Back</a> </td></tr> <tr><td colspan="2" align="center"> <?php echo $Message ?> </td></tr> </table> </form> </body> </html>
Regards,
Alf Stockton www.stockton.co.za
Alf Stockton www.stockton.co.za
Stockton,
You can't use
Use
To hide/show a
where
You can't use
<div> like that to segment part(s) of a table. Use
<tbody> instead. You may have as many <tbody>s as you like within a table, each containing any number of <tr>s , each containing any number of <td>s .<td>s may contain <div>s but do not generally need to as each <td> is directly addressable by giving it an id.To hide/show a
<tbody> with JavaScript, use: javascript Syntax (Toggle Plain Text)
tttt.style.display = 'none'; tttt.style.display = '';
tttt is a reference to a particular <tbody> obtained (for example) with document.getElementById(...) . Last edited by Airshow; Sep 1st, 2009 at 7:15 pm.
50% of the solution lies in accurately describing the problem!
•
•
Join Date: Jul 2008
Posts: 25
Reputation:
Solved Threads: 0
Thank you for your suggestion and now I almost have it the way I want but ......
Initial display is fine and if the user switches from "Parent" to "Dependent" the additional fields do display as expected but if they then switch from "Dependent" to "Parent" the additional fields do not disappear.
BTW The code I am working on can be seen at http://www.stockton.co.za/HelpTest.php
Obviously I am missing something, but what ?
Initial display is fine and if the user switches from "Parent" to "Dependent" the additional fields do display as expected but if they then switch from "Dependent" to "Parent" the additional fields do not disappear.
BTW The code I am working on can be seen at http://www.stockton.co.za/HelpTest.php
Obviously I am missing something, but what ?
Regards,
Alf Stockton www.stockton.co.za
Alf Stockton www.stockton.co.za
Stockton,
That's because the value of the selected option is detemined by its
You will find the following code more reliable cross-browser. It is also reusable in that it takes arguments rather than relying on hard-code.
I notice you are using DOCTYPE XHTML strict which means that, if you want the page to validate, all HTML tags should balance (or end in />), all tagnames and attributes should be lower case, and (iirc) attribute values should be in double quotes.
Airshow
That's because the value of the selected option is detemined by its
value attribute (0|1) not its innerHTML property (Parent|Dependant).You will find the following code more reliable cross-browser. It is also reusable in that it takes arguments rather than relying on hard-code.
javascript Syntax (Toggle Plain Text)
function showhidefield(menu, elId){ var el = document.getElementById(elId); if(el){ el.style.display = (menu[menu.selectedIndex].value == '0') ? 'none' : ''; } }
HTML Syntax (Toggle Plain Text)
<select id="RecordType" name="RecordType" onchange="showhidefield(this, 'hideablearea')"> <option selected value="0">Parent</option> <option value="1">Dependant</option> </select>
Airshow
Last edited by Airshow; Sep 2nd, 2009 at 1:27 pm.
50% of the solution lies in accurately describing the problem!
![]() |
Similar Threads
- how to populate a cfselect box depending upon the selection on raido button (ColdFusion)
- Save form values - show/hide divs depending on values (JavaScript / DHTML / AJAX)
- hide/unhide fields depending upon other fields. (JavaScript / DHTML / AJAX)
- Calling a Javascript Function in InnerHTML (JavaScript / DHTML / AJAX)
- populating dropdownlist based on gridview selection (ASP.NET)
- Using cfcheck! (ColdFusion)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Embid php to javascript
- Next Thread: Trim Function
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically beta box browser calendar captchaformproblem cart close codes column css cursor date debugger decimal dependent design dom download dropdown element embed enter error events firefox focus form frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 iframe image() index java javascript javascripthelp2020 jawascriptruntimeerror jquery jsp libcurl listbox maps masterpage media menu microsoft mimic mp4 onerror onmouseover paypal php player position post problem programming prototype rated rating redirect regex safari scale scriptlets search security select software sql starrating text textarea toggle unicode validation variables w3c webservice website window windowofwords windowsxp





