943,699 Members | Top Members by Rank

Ad:
Sep 1st, 2009
0

Hide fields depending on selection

Expand Post »
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?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta http-equiv="Content-Style-Type" content="text/css" />
  8. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  9. <title>Display and Hide</title>
  10. <script language="JavaScript">
  11. function showhidefield()
  12. {
  13. if (document.HelpAdd.RecordType.value == "Parent")
  14. {
  15. document.getElementById("hideablearea").style.visibility = "hidden";
  16. }
  17. else
  18. {
  19. document.getElementById("hideablearea").style.visibility = "visible";
  20. }
  21. }
  22. </script>
  23.  
  24. </head>
  25. <body>
  26. <form name='HelpAdd' action='nextpage.asp'>
  27. <table align="center" border="3" cellspacing="0" cellpadding="3">
  28. <tr><td>Record Type:</td>
  29. <td><select id="RecordType" name="RecordType" onchange=showhidefield()>
  30. <option value='0'>Parent</option>
  31. <option value='1'>Dependant</option><br/>
  32. <tr><td>Topic Title:</td>
  33. <td><input type="text" name="TopicName" ID="TopicName" maxlength="25"></td></tr>
  34.  
  35. <div id='hideablearea' style='visibility:hidden;'>
  36. <tr><td>Parent:</td>
  37. <td><select name="Parent">
  38. <input type="text" name="ParentID" ID="ParentID" maxlength="25"></td></tr>
  39. <tr><td>Topic Body:</td>
  40. <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $MaxFileSize ?>" />
  41. <td><input id="helpfile" name="helpfile" type="file" /></td></tr>
  42. </div>
  43. <tr><td colspan="2" align="center">
  44. <input type="submit" name="submit" value="Submit">
  45. <a href="/help.php"><button>Back</a>
  46. </td></tr>
  47. <tr><td colspan="2" align="center">
  48. <?php echo $Message ?>
  49. </td></tr>
  50. </table>
  51. </form>
  52. </body>
  53. </html>
Similar Threads
Reputation Points: 20
Solved Threads: 0
Light Poster
stockton is offline Offline
32 posts
since Jul 2008
Sep 1st, 2009
0

Re: Hide fields depending on selection

Stockton,

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)
  1. tttt.style.display = 'none';
  2. tttt.style.display = '';
where 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.
Sponsor
Reputation Points: 302
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,524 posts
since Apr 2009
Sep 2nd, 2009
0

Re: Hide fields depending on selection

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 ?
Reputation Points: 20
Solved Threads: 0
Light Poster
stockton is offline Offline
32 posts
since Jul 2008
Sep 2nd, 2009
1

Re: Hide fields depending on selection

Stockton,

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)
  1. function showhidefield(menu, elId){
  2. var el = document.getElementById(elId);
  3. if(el){ el.style.display = (menu[menu.selectedIndex].value == '0') ? 'none' : ''; }
  4. }
HTML Syntax (Toggle Plain Text)
  1. <select id="RecordType" name="RecordType" onchange="showhidefield(this, 'hideablearea')">
  2. <option selected value="0">Parent</option>
  3. <option value="1">Dependant</option>
  4. </select>
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
Last edited by Airshow; Sep 2nd, 2009 at 1:27 pm.
Sponsor
Reputation Points: 302
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,524 posts
since Apr 2009
Sep 3rd, 2009
0

Re: Hide fields depending on selection

Excellent. Just what I needed. Thank you.
Reputation Points: 20
Solved Threads: 0
Light Poster
stockton is offline Offline
32 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Embid php to javascript
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Trim Function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC