Walter Lee Davis wrote:
> On Apr 24, 2009, at 1:38 AM, katz wrote:
>
>
>> On Event observers, I just realized it wasn't working because the html
>> was only inserted upon calling "Add new item variation."
>>
>> So it would definitely fail regardless of whether
>>
>> document.observe('dom:loaded',function() or
>> Event.observe(window,'load',function() is used.
>>
>> That's something I didn't see earlier.
>> Adding the script below the button works fine anyway.
>>
>
>
> Read up on event delegation, that can save you a lot of specific code
> (and duplication). Basically, you take advantage of the fact that
> events usually bubble up until something catches them. So observe a
> click on the document, or on some other element that's higher up the
> DOM tree than your inserted element, and even when the click hits the
> child of that element, the parent will "hear" it.
>
> <div id="form_container">
> //buncha dynamic form elements in here
> <input type="button" class="copy" value="Copy" id="button_3" />
> (that button could be there in source, might be added later by JS,
> doesn't matter for the following to work)
> </div>
>
> //script
> $('form_container').observe('click',function(evt){
> var elm = evt.element();
> if(elm.hasClassName('copy')){
> //do your copy stuff here, using DOM traversal to work out What
> to
> copy Where
> }
> if(elm.hasClassName('somethingElse')){
> //another behavior here
> }
> ...
> });
>
> Walter
>
Thanks. Yeah right now there's duplication.
I simply used Ruby code to set a unique for each field.
Something like:
<script type="text/javascript">
$('same_<%= variation.id %>').observe('click', function(e) {
if ($('same_<%= variation.id %>').checked){
$('variation_price_<%= variation.id
%>').setValue($F('product_price'));
}
else {
$('variation_price_<%= variation.id %>').setValue('');
}
});
</script>
And this repeats every time the insert_html RJS code is called.
It works however.
I'll read on event delegation as well later.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---