The Drupal.beahviors.my_module = function() {<br>//Code<br>};<br><br>is really just a replacement of the $(document).ready(function(){<br>}); jquery wrapper that executes your jquery code when the document is ready.<br><br>
To bind an action to an elements change event use something like this within your function.<br><br>$("#my-element-selector").bind("change", function(){<br>//do you actions here<br>});<br><br>As a note the change event is really on valid on things like select elements and I think radios/chackboxes, but doesn't work on textfields/areas.<br clear="all">
-----<br>Adam A. Gregory<br>Drupal Developer & Consultant<br>Web: AdamAGregory.com<br>Twitter: <a href="http://twitter.com/adamgregory">twitter.com/adamgregory</a><br>Phone: 910.808.1717<br>Cell: 706.761.7375<br>
<br><br><div class="gmail_quote">On Sun, Feb 21, 2010 at 7:43 PM, Karyn Cassio <span dir="ltr"><<a href="mailto:karyn@karyncassio.com">karyn@karyncassio.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Alrighty, now I'm home and actually looking at the code surrounded by books.<br>
I assume the code below (with modifications of course) should be replacing my javaScript code.<br>
However, I'm not sure how I call this from my form.<br>
I was calling the javascript with an onchange attribute of a textfield.<br>
(BTW, all Earl's assumptions below were exactly correct.)<br>
<br>
The name of my module is compute_items. This is how I stated the jquery function.<br>
Drupal.behaviors.compute_items = function() {<br>
//code stuff<br>
}<br>
<br>
Is that the right direction?<br>
<br>
Karyn<br>
<br>
On 2/21/10 2:22 PM, Earl Miles wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Karyn Cassio wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi Everyone,<br>
<br>
I hope you are enjoying your weekend and not working too hard.<br>
<br>
I'm still trying to get my head around js, and was hoping someone may have some insights to help me with the next steps on a form I am creating.<br>
<br>
I have an order form that is going to be dynamically created.<br>
I am able to successfully pass the quantity value upon user input for field 1.<br>
Basically I'm using onchange() to pass the quantity value to the script.<br>
The id of the price field is of the format unit-price-0, unit-price-1, etc.<br>
Each line item's subtotal should update before going onto the next line item.<br>
<br>
Here's the js function. Each line item has its own unit-cost, quantity, and subtotal.<br>
Any help would be so appreciated.<br>
</blockquote>
<br>
Let's assume that you've set up the ids so that they all match:<br>
<br>
1) Each line item has a unique, probably numeric id.<br>
2) The id of all the form fields for each line item is carefully<br>
crafted such that the quantity will be "edit-quantity-ID", the<br>
price will be "edit-price-ID" and the subtotal will be<br>
"edit-subtotal-ID"<br>
3) The quantity is a textfield. The price is either a disabled textfield<br>
so that the user cannot change it, or a hidden field. The subtotal<br>
is similar.<br>
4) Each line item quantity has this class: 'line-item-quantity'.<br>
<br>
You would then create a .js file using this code. Note that I am using jQuery to create simpler code, so if you're not familiar with jQuery it may not be immediately understandable, but it would be very valuable to learn some as use of jQuery significantly increases javascript productivity.<br>
<br>
For example's sake I am butchering the calculation to be as simple as possible. Judging from your code it needs to be a touch more complex than this, but you can handle that I am sure.<br>
----<br>
<br>
// You would put this inside a Drupal behavior:<br>
Drupal.behaviors.myBehaviorFunction = function() {<br>
$('.line-item-quantity:not(.line-item-processed)')<br>
.addClass('line-item-processed') // Ensures we do only process once<br>
.change(function() {<br>
// The id of this should be edit-quantity-ID so we use replace<br>
// to get the base id:<br>
var id = $(this).attr('id').replace('edit-quantity-', '');<br>
var price = $('edit-price-' + id).val();<br>
<br>
var subTotal = id * price; // Do your actual calculation here.<br>
<br>
$('edit-subtotal-' + id).val(subTotal);<br>
});<br>
};<br>
</blockquote>
<br>
<br>
-- <br>
Karyn Cassio<br>
Drupal Developer<br>
303-981-4161<br>
<br>
</blockquote></div><br>