473,396 Members | 1,768 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,396 software developers and data experts.

Trying to get 2 Javascripts working together

So I am not great at Javascript, but i have 2 scripts that i want to work with each other. When i use one by itself, they each work fine, but when i add them together it does not work.

I have a user form with a bunch of fields on it. If they choose an option on the drop down menu, some fields either show or disappear. Those fields have an "On Change" event that subtracts one field form another and gives us a total.
////////////////////////////////////// Drop Down Menu /////
Expand|Select|Wrap|Line Numbers
  1. rel sets to show the <tr>
  2. <select name="checkout_by" class="textLeft" id="checkout_by" value="<?php if($cont == 'no') { echo $checkout_by; } ?>" > <option value="serial" selected rel="show">Item Series / Serial Number</option>
  3. <option value="isbn" rel="none">Barcode / ISBN Number</option>
//////////////////////////////////// Fields used to subtract

Expand|Select|Wrap|Line Numbers
  1. <input name="serial_start" onChange="updatesum()" type="text" class="textLeft" id="serial_start" value="<?php if($cont == 'no') { echo $serial_start; } ?>" size="15">

Expand|Select|Wrap|Line Numbers
  1. <input name="serial_end" onChange="updatesum()" type="text" class="textLeft" id="serial_end" value="<?php if($cont == 'no') { echo $serial_end; } ?>" size="15">

//////////////////////////////////// Field used to add the total up from above

Expand|Select|Wrap|Line Numbers
  1. <input name="org_quantity" type="text" id="org_quantity" value="<?php if($cont == 'no') { echo $orig_quantity; } ?>" size="10">
  2.  


///////////////////////////////////////Update total
Expand|Select|Wrap|Line Numbers
  1. script
  2. <script type="text/javascript">
  3. function updatesum() {
  4. document.form1.org_quantity.value = (document.form1.serial_end.value -0) - (document.form1.serial_start.value -0);
  5. }
  6. </script>

//////////////////////////////////// Hide Filed Script
Expand|Select|Wrap|Line Numbers
  1. var containerTag = 'TR';
  2.  
  3. var compatible = (
  4. document.getElementById && document.getElementsByTagName && document.createElement
  5. &&
  6. !(navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1)
  7. );
  8.  
  9. if (compatible)
  10. {
  11. document.write('<style>.accessibility{display: none}</style>');
  12. var waitingRoom = document.createElement('div');
  13. }
  14.  
  15. var hiddenFormFieldsPointers = new Object();
  16.  
  17. function prepareForm()
  18. {
  19. if (!compatible) return;
  20. var marker = document.createElement(containerTag);
  21. marker.style.display = 'none';
  22.  
  23. var x = document.getElementsByTagName('select');
  24. for (var i=0;i<x.length;i++)
  25. addEvent(x[i],'change',showHideFields)
  26.  
  27. var x = document.getElementsByTagName(containerTag);
  28. var hiddenFields = new Array;
  29. for (var i=0;i<x.length;i++)
  30. {
  31. if (x[i].getAttribute('rel'))
  32. {
  33. var y = getAllFormFields(x[i]);
  34. x[i].nestedRels = new Array();
  35. for (var j=0;j<y.length;j++)
  36. {
  37. var rel = y[j].getAttribute('rel');
  38. if (!rel || rel == 'none') continue;
  39. x[i].nestedRels.push(rel);
  40. }
  41. if (!x[i].nestedRels.length) x[i].nestedRels = null;
  42. hiddenFields.push(x[i]);
  43. }
  44. }
  45.  
  46. while (hiddenFields.length)
  47. {
  48. var rel = hiddenFields[0].getAttribute('rel');
  49. if (!hiddenFormFieldsPointers[rel])
  50. hiddenFormFieldsPointers[rel] = new Array();
  51. var relIndex = hiddenFormFieldsPointers[rel].length;
  52. hiddenFormFieldsPointers[rel][relIndex] = hiddenFields[0];
  53. var newMarker = marker.cloneNode(true);
  54. newMarker.id = rel + relIndex;
  55. hiddenFields[0].parentNode.replaceChild(newMarker,hiddenFields[0]);
  56. waitingRoom.appendChild(hiddenFields.shift());
  57. }
  58.  
  59. setDefaults();
  60. addEvent(document,'click',showHideFields);
  61. }
  62.  
  63. function setDefaults()
  64. {
  65. var y = document.getElementsByTagName('input');
  66. for (var i=0;i<y.length;i++)
  67. {
  68. if (y[i].checked && y[i].getAttribute('rel'))
  69. intoMainForm(y[i].getAttribute('rel'))
  70. }
  71.  
  72. var z = document.getElementsByTagName('select');
  73. for (var i=0;i<z.length;i++)
  74. {
  75. if (z[i].options[z[i].selectedIndex].getAttribute('rel'))
  76. intoMainForm(z[i].options[z[i].selectedIndex].getAttribute('rel'))
  77. }
  78.  
  79. }
  80.  
  81. function showHideFields(e)
  82. {
  83. if (!e) var e = window.event;
  84. var tg = e.target || e.srcElement;
  85.  
  86. if (tg.nodeName == 'LABEL')
  87. {
  88. var relatedFieldName = tg.getAttribute('for') || tg.getAttribute('htmlFor');
  89. tg = document.getElementById(relatedFieldName);
  90. }
  91.  
  92. if (
  93. !(tg.nodeName == 'SELECT' && e.type == 'change')
  94. &&
  95. !(tg.nodeName == 'INPUT' && tg.getAttribute('rel'))
  96. ) return;
  97.  
  98. var fieldsToBeInserted = tg.getAttribute('rel');
  99.  
  100. if (tg.type == 'checkbox')
  101. {
  102. if (tg.checked)
  103. intoMainForm(fieldsToBeInserted);
  104. else
  105. intoWaitingRoom(fieldsToBeInserted);
  106. }
  107. else if (tg.type == 'radio')
  108. {
  109. removeOthers(tg.form[tg.name],fieldsToBeInserted)
  110. intoMainForm(fieldsToBeInserted);
  111. }
  112. else if (tg.type == 'select-one')
  113. {
  114. fieldsToBeInserted = tg.options[tg.selectedIndex].getAttribute('rel');
  115. removeOthers(tg.options,fieldsToBeInserted);
  116. intoMainForm(fieldsToBeInserted);
  117. }
  118. }
  119.  
  120. function removeOthers(others,fieldsToBeInserted)
  121. {
  122. for (var i=0;i<others.length;i++)
  123. {
  124. var show = others[i].getAttribute('rel');
  125. if (show == fieldsToBeInserted) continue;
  126. intoWaitingRoom(show);
  127. }
  128. }
  129.  
  130. function intoWaitingRoom(relation)
  131. {
  132. if (relation == 'none') return;
  133. var Elements = hiddenFormFieldsPointers[relation];
  134. for (var i=0;i<Elements.length;i++)
  135. {
  136. waitingRoom.appendChild(Elements[i]);
  137. if (Elements[i].nestedRels)
  138. for (var j=0;j<Elements[i].nestedRels.length;j++)
  139. intoWaitingRoom(Elements[i].nestedRels[j]);
  140. }
  141. }
  142.  
  143. function intoMainForm(relation)
  144. {
  145. if (relation == 'none') return;
  146. var Elements = hiddenFormFieldsPointers[relation];
  147. for (var i=0;i<Elements.length;i++)
  148. {
  149. var insertPoint = document.getElementById(relation+i);
  150. insertPoint.parentNode.insertBefore(Elements[i],insertPoint);
  151. if (Elements[i].nestedRels)
  152. {
  153. var fields = getAllFormFields(Elements[i]);
  154. for (var j=0;j<fields.length;j++)
  155. {
  156. if (!fields[j].getAttribute('rel')) continue;
  157. if (fields[j].checked || fields[j].selected)
  158. intoMainForm(fields[j].getAttribute('rel'));
  159. }
  160. }
  161. }
  162. }
  163.  
  164. function getAllFormFields(node)
  165. {
  166. var allFormFields = new Array;
  167. var x = node.getElementsByTagName('input');
  168. for (var i=0;i<x.length;i++)
  169. allFormFields.push(x[i]);
  170. var y = node.getElementsByTagName('option');
  171. for (var i=0;i<y.length;i++)
  172. allFormFields.push(y[i]);
  173. return allFormFields;
  174. }
  175.  
  176. /** ULTRA-SIMPLE EVENT ADDING **/
  177.  
  178. function addEvent(obj,type,fn)
  179. {
  180. if (obj.addEventListener)
  181. obj.addEventListener(type,fn,false);
  182. else if (obj.attachEvent)
  183. obj.attachEvent("on"+type,fn);
  184. }
  185.  
  186. addEvent(window,"load",prepareForm);
  187.  
  188.  
  189. /** PUSH AND SHIFT FOR IE5 **/
  190.  
  191. function Array_push() {
  192. var A_p = 0
  193. for (A_p = 0; A_p < arguments.length; A_p++) {
  194. this[this.length] = arguments[A_p]
  195. }
  196. return this.length
  197. }
  198.  
  199. if (typeof Array.prototype.push == "undefined") {
  200. Array.prototype.push = Array_push
  201. }
  202.  
  203. function Array_shift() {
  204. var A_s = 0
  205. var response = this[0]
  206. for (A_s = 0; A_s < this.length-1; A_s++) {
  207. this[A_s] = this[A_s + 1]
  208. }
  209. this.length--
  210. return response
  211. }
  212.  
  213. if (typeof Array.prototype.shift == "undefined") {
  214. Array.prototype.shift = Array_shift
  215. }
Aug 3 '10 #1
1 1475
dlite922
1,584 Expert 1GB
Use firefox. Open Tools-> Error Console.

Do you see any errors for your site?

(Hit the red circle (delete) and access your page again if there are too many errors)

What is the first error you see?

Dan
Aug 4 '10 #2

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

Similar topics

1
by: mackenzie | last post by:
Hi, I found a great js calendar pop-up that i want to use on the same page where i'm already using a .vbs for client- side script. I can use either one individually w/o problems but, when I try...
0
by: Sokolov Yura | last post by:
Django Model is wonderfull. But SQLObject more flexible (and powerfull, as i think, and has already more db interfaces). But Django Model is tied with Django, and using Django with another OO...
13
by: David L Wright II | last post by:
Does anyone know when building a msi file under VB.Net 2003, it would try to install something from the VS 2005 Beta 1? I have both products installed. Thanks,
2
by: phil cunningham | last post by:
I am using Access to run a database within the company and also have a website that makes some of the information available to the general public And so far everything is working very well q....
0
by: Rod | last post by:
Well, I've taken the plunge and gotten myself my first domain. (I've worked on other peoples and of course at work, but never done my own domain.) With this new project my family and I will...
4
by: geshan | last post by:
In php5 I am not able to use include funciton and header("location:""" together please help.
2
by: buddyr | last post by:
Hello, I am having trouble with two combo boxes working together. two tables table1 table2 table1 fields -number and name table 2 fields -code and number ...
4
by: MimiMi | last post by:
I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly! I wouldn't be surprised if my problems have something to do with...
0
by: VicJ | last post by:
I'm trying to make a control on which the user can freely drag an image with the mouse and zoom and rotate it with the mouse wheel. I'd like to do this as far as possible using graphics transforms....
3
by: SavRak | last post by:
Hi, I seem to be having an issue linking my XML document and XML Schema together. Both are not working, and do not seem to be validating when using: http://tools.decisionsoft.com/schemaValidate/ ...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.