[Javascript] Targeting the correct form??

Terry Riegel riegel at clearimageonline.com
Wed Feb 25 10:20:23 CST 2009


Hello everyone. I am struggling with a particular function. I have a  
page with multiple forms on it. I am applying unobtrusive javascript  
to it and am having difficulty targeting the right form with my code.  
Via an onload I call forms() and that works great. When I go to submit  
the form I simply alert the id of the form submitted. At least thats  
what I would think my code would do (listed below). Instead regardless  
of what form button I click on It alerts the id of the last form  
element on the page.

Any help will be greatly appreciated.


function forms(h) {
  var daItems = $$('.'+h);
  for (iItem=0; iItem < daItems.length; iItem++) {
    var o=daItems[iItem]
    o.onsubmit=function(){return doforms(o)};
   }
  }

  function doforms(o) {
  alert(o.id);
  return false;
  }



The Markup for the three forms look like this...


<form ACTION="http://www.positiveaction.org/pages/htmlos/9744.92.2889966640720767219 
" method="post" class="cartform" id="FORMDESCRPT1100">
  <input type="hidden" name="cartcontrol"  
value="w_zFL9VA5198CBA8FNFYJNJYONNMSZNUSRRUWRiYX">
  <input type="image" name="buttoncart" src="/newsite/images/button- 
cart.png" width="160" height="24">
  <input type="text" name="productrpt1100" size="2" style="width:  
24px;">
</form>

<form ACTION="http://www.positiveaction.org/pages/htmlos/9744.92.2939330254420767219 
" method="post" class="cartform" id="FORMDESCu-RPT1100">
  <input type="hidden" name="cartcontrol"  
value="MgUmse2haWedjihdmum5quq5v6spzyyx3-y5322572H98">
  <input type="image" name="buttoncart" src="/newsite/images/button- 
used.png" width="160" height="24">
  <input type="text" name="productu_rpt1100" size="2" style="width:  
24px;">
</form>

   <form ACTION="http://www.positiveaction.org/pages/htmlos/9744.92.3421382275720767219 
" method="post" class="cartform" id="FORMsmallcart">
  <input type="hidden" name="cartcontrol"  
value="GaOgmYwbUQXjcYcjhoeLhkgzkQkrrY1z645H-Mvj142rG0176">
  <input type=text name="cart[24,1]" size="3" style="width: 24px;"  
VALUE="17">
  <input type=text name="cart[24,2]" size="3" style="width: 24px;"  
VALUE="1">
  <input type="image" name="updatecart" src="/newsite/images/ 
updatecart.png" width="95" height="24">
  <input type="image" name="checkoutcart" src="/newsite/images/ 
checkoutcart.png" width="95" height="24">
</form>




More information about the Javascript mailing list