473,410 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

onchange only triggered on refresh when added through javascript

Trying to hide/show some form field based on select choice. It works just fine in my test setup. In the real world example I have to add the onchange event to my select box with
Expand|Select|Wrap|Line Numbers
  1. selectBoxId1.onchange(hideField(selectBoxId1,selectBoxValue1,hiddenBoxId1));
For some reason when doing it this way the function only runs when the page is refreshed. Any ideas on how I can solve this?

in the head
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javaScript">
  2.  
  3. function hideField(selectBox,selectBoxValue,hiddenBox)
  4. {    
  5. if(selectBox.options[selectBox.selectedIndex].value == selectBoxValue)
  6. {
  7. //Show the textbox
  8. hiddenBox.style.visibility = 'visible';
  9. hiddenBox.style.display = '';
  10. alert('show');
  11. }
  12. else
  13. {
  14. //Hide the textbox
  15. hiddenBox.style.visibility = 'hidden';
  16. hiddenBox.style.display = 'none';
  17.  
  18. alert('hide');
  19. }
  20.  
  21. }
  22. </script>
in the body
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var selectBoxId1 = document.getElementById('Type-of-Property');
  3. var selectBoxValue1 = 'Other(please state)';
  4. var hiddenBoxId1 = document.getElementById('li-2-7');
  5. selectBoxId1.onchange(hideField(selectBoxId1,selectBoxValue1,hiddenBoxId1));
The form
Expand|Select|Wrap|Line Numbers
  1.     <form enctype="multipart/form-data" action="/#usermessage2" 
  2. method="post" class="cform" id="cforms2form">
  3.         <ol class="cf-ol">
  4.             <li id="li-2-1" class="textonly"><h2>This is the final part of the 
  5. form</h2></li>
  6.         </ol>
  7.         <fieldset class="cf-fs1">
  8.         <legend>Property Details</legend>
  9.         <ol class="cf-ol">
  10.             <li id="li-2-3" class=""><label id="label-2-3" for="Address"><span>Address</span></label><input
  11.  name="Address" id="Address" class="single" type="text"></li>
  12.             <li id="li-2-4" class=""><label id="label-2-4" for="Post-Code"><span>Post
  13.  Code</span></label><input name="Post-Code" id="Post-Code" 
  14. class="single" type="text"></li>
  15.             <li id="li-2-5" class=""><label id="label-2-5" for="No.-of-Bedrooms"><span>No.
  16.  of Bedrooms</span></label><input name="No.-of-Bedrooms" 
  17. id="No.-of-Bedrooms" class="single" type="text"></li>
  18.             <li id="li-2-6" class=""><label id="label-2-6" for="Type-of-Property"><span>Type
  19.  of Property</span></label><select name="Type-of-Property" 
  20. id="Type-of-Property" class="cformselect">
  21.                 <option value="Please select…">Please select…</option>
  22.                 <option value="Flat">Flat</option>
  23.                 <option value="Semi Detatched House">Semi Detatched House</option>
  24.                 <option selected="selected" value="Detatched House">Detatched House</option>
  25.                 <option value="Bungalow">Bungalow</option>
  26.                 <option value="Mid Terrace">Mid Terrace</option>
  27.                 <option value="End terrace">End terrace</option>
  28.                 <option value="linked Detatched">linked Detatched</option>
  29.                 <option value="Maisonette">Maisonette</option>
  30.                 <option value="Other(please state)">Other(please state)</option>
  31.             </select></li>
  32.             <li style="visibility: hidden; display: none;" id="li-2-7" class=""><label
  33.  id="label-2-7" for="Type-of-Property-(Other)"><span>Type of Property 
  34. (Other)</span></label><input name="Type-of-Property-(Other)" 
  35. id="Type-of-Property-(Other)" class="single" type="text"></li>
  36.         </ol>
  37.         </fieldset>
  38.         <fieldset class="cf-fs2">
  39.         <legend>Condition Of Property</legend>
  40.         <ol class="cf-ol">
  41.             <li id="li-2-9" class=""><label id="label-2-9" 
  42. for="Condition-of-Property"><span>Condition of Property</span></label><select
  43.  name="Condition-of-Property" id="Condition-of-Property" 
  44. class="cformselect">
  45.                 <option selected="selected" value="Please select…">Please select…</option>
  46.                 <option value="Excellent">Excellent</option>
  47.                 <option value="Good">Good</option>
  48.                 <option value="Average">Average</option>
  49.                 <option value="Poor/In need of repair">Poor/In need of repair</option>
  50.             </select></li>
  51.             <li id="li-2-10" class=""><label id="label-2-10" 
  52. for="Please-describe-your-situation"><span>Please describe your 
  53. situation</span></label><textarea style="border: 1px solid rgb(226, 226,
  54.  222);" cols="30" rows="8" name="Please-describe-your-situation" 
  55. id="Please-describe-your-situation" class="area"></textarea><div 
  56. id="dCField1" style="height: 50px; font-size: 11px;"><b>200</b> 
  57. characters remaining in your input limit.</div></li>
  58.             <li id="li-2-11" class=""><label id="label-2-11" for="Property-value"><span>Property
  59.  value</span></label><input name="Property-value" id="Property-value" 
  60. class="single" type="text"></li>
  61.         </ol>
  62.         </fieldset>
  63.         <fieldset class="cf-fs3">
  64.         <legend>Financial Information</legend>
  65.         <ol class="cf-ol">
  66.             <li id="li-2-13" class=""><label id="label-2-13" for="Value-Based-On"><span>Value
  67.  Based On</span></label><select name="Value-Based-On" 
  68. id="Value-Based-On" class="cformselect">
  69.                 <option selected="selected" value="Please select…">Please select…</option>
  70.                 <option value="Estate agent valuation">Estate agent valuation</option>
  71.                 <option value="Mortgage valuation">Mortgage valuation</option>
  72.                 <option value="Surveyor">Surveyor</option>
  73.                 <option value="Own opinion">Own opinion</option>
  74.                 <option value="Other(please state)">Other(please state)</option>
  75.             </select></li>
  76.             <li id="li-2-14" class=""><label id="label-2-14" 
  77. for="Value-Based-On-(Other)"><span>Value Based On (Other)</span></label><input
  78.  name="Value-Based-On-(Other)" id="Value-Based-On-(Other)" 
  79. class="single" type="text"></li>
  80.             <li id="li-2-15" class=""><label id="label-2-15" 
  81. for="Mortgage-Amount-outstanding"><span>Mortgage Amount outstanding </span></label><input
  82.  name="Mortgage-Amount-outstanding" id="Mortgage-Amount-outstanding" 
  83. class="single" type="text"></li>
  84.             <li id="li-2-16" class=""><label id="label-2-16" for="Secured-loans:"><span>Secured
  85.  loans:</span></label><input name="Secured-loans:" id="Secured-loans:" 
  86. class="single" type="text"></li>
  87.             <li id="li-2-17" class=""><label id="label-2-17" 
  88. for="Monthly-cost-of-Mortgage:"><span>Monthly cost of Mortgage:</span></label><input
  89.  name="Monthly-cost-of-Mortgage:" id="Monthly-cost-of-Mortgage:" 
  90. class="single" type="text"></li>
  91.             <li id="li-2-18" class=""><label id="label-2-18" 
  92. for="How-quickly-do-you-need-to-sell?"><span>How quickly do you need to 
  93. sell?</span></label><select name="How-quickly-do-you-need-to-sell?" 
  94. id="How-quickly-do-you-need-to-sell?" class="cformselect">
  95.                 <option selected="selected" value="Please select…">Please select…</option>
  96.                 <option value="Urgent. Need to Sell Now">Urgent. Need to Sell Now</option>
  97.                 <option value="2 Weeks">2 Weeks</option>
  98.                 <option value="1 Month">1 Month</option>
  99.                 <option value="2 - 3 Months">2 - 3 Months</option>
  100.                 <option value="Not urgent">Not urgent</option>
  101.             </select></li>
  102.             <li id="li-2-19" class=""><label id="label-2-19" 
  103. for="Are-you-facing-Repossesion?"><span>Are you facing Repossesion?</span></label><select
  104.  name="Are-you-facing-Repossesion?" id="Are-you-facing-Repossesion?" 
  105. class="cformselect">
  106.                 <option selected="selected" value="Please select...">Please 
  107. select...</option>
  108.                 <option value="Yes">Yes</option>
  109.                 <option value="No">No</option>
  110.             </select></li>
  111.             <li id="li-2-20" class=""><label id="label-2-20" 
  112. for="Any-further-information"><span>Any further information</span></label><textarea
  113.  cols="30" rows="8" name="Any-further-information" 
  114. id="Any-further-information" class="area"></textarea></li>
  115.         </ol>
  116.         </fieldset>
  117.         <fieldset class="cf-fs4">
  118.         <legend>Verification</legend>
  119.         <ol class="cf-ol">
  120.             <li id="li-2-22" class=""><label id="label-2-22" for="cforms_q2" 
  121. class="secq"><span>The colour of grass is</span></label><input 
  122. name="cforms_q2" id="cforms_q2" class="secinput " type="text"></li>
  123.         </ol>
  124.         </fieldset>
  125.         <fieldset class="cf_hidden">
  126.             <legend>&nbsp;</legend>
  127.             <input name="cforms_a2" id="cforms_a2" 
  128. value="9f27410725ab8cc8854a2769c7a516b8" type="hidden">
  129.             <input name="cf_working2" id="cf_working2" 
  130. value="One%20moment%20please..." type="hidden">
  131.             <input name="cf_failure2" id="cf_failure2" value="" type="hidden">
  132.             <input name="cf_codeerr2" id="cf_codeerr2" 
  133. value="Please%20double-check%20your%20verification%20code." 
  134. type="hidden">
  135.             <input name="cf_customerr2" id="cf_customerr2" value="yyy" 
  136. type="hidden">
  137.             <input name="cf_popup2" id="cf_popup2" value="nn" type="hidden">
  138.         </fieldset>
  139.         <p class="cf-sb"><input name="sendbutton2" id="sendbutton2" 
  140. class="sendbutton" value="" type="submit"></p></form>
Jun 24 '10 #1
1 1858
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. selectBoxId1.onchange(hideField(selectBoxId1,selectBoxValue1,hiddenBoxId1));
that’s invalid syntax.

Expand|Select|Wrap|Line Numbers
  1. function handler(evt)
  2. {
  3.     evt = evt || window.event; // IE workaround
  4.     // your code here
  5. }
  6. element.onevent = handler;
or in all W3C compliant browsers
Expand|Select|Wrap|Line Numbers
  1. element.addEventListener("event", handler, false); // set true for capturing mode
Jun 24 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: IkBenHet | last post by:
Hello, Currently I am using a large input form on a website that is based on ASP and JavaScript. Depending on the values that are filled in by the user the forms does a refresh and makes...
3
by: anthonybrough | last post by:
I have an asp page that has a form to collect user data in a form. when the user clicks submit the input is validated. If any fields are not acceptable the user clicks on a button to go back to...
5
by: mattcolenutt | last post by:
Scenario: Have a product form with an "image location" field. The image property uses this field to display the image. Problem: If an image location field has been changed, the image will not...
6
by: RobGMiller | last post by:
Using ASP.NET 2.0 Need to simply process incoming postback information without sending back a reponse. Client sends HTML from application other than a browser to a page on a web site. Web...
2
by: srinivasyadavkota | last post by:
Hi Experts here, Pls help. first I set the following in IE6 :Under Tools/Internet Options... 1) Temporary Internet file 2) Check for newer versions of...
8
by: jrayjr | last post by:
Im work on a form that will dynamicly display objects. The form is completed and working but am adding some features that will appear based on the user selection. I am unable to get the onchange...
2
by: qwedster | last post by:
Folks! The following is a "Hello World" kind of code for ViewState. I just want to know how to retain the ViewState 1) while Page Refresh when using UpdatePanel and also 2) While I reverting back...
4
by: ganihskmca | last post by:
The error im getting is object expected. This is my jsp page: <%@ page import="java.sql.*" %> <% String connectionURL = "jdbc:mysql://localhost/new"; Connection connection = null; Statement...
24
by: Cainnech | last post by:
Hello, I'm trying to create an application where I can extract information from a page and put it into Excel. Now the problem is that the page has been constructed using jQuery and Ajax. So...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.