Here's one quick way to cut down on duplication:

<script type="text/javascript" charset="utf-8">
    function applyCheckboxBehavior(checkboxes, toggleControl) {
      toggleControl.observe('click', function() {
        checkboxes.each( function(box) {
          box.checked = toggleControl.checked;
        });
      });
    }

    applyCheckboxBehavior($$("input[id=cluster_code_]"),
     $("all_cluster_selector"));

    applyCheckboxBehavior($$("input[id=hosp_code_]"),
     $("all_hospital_selector"));
</script>

(Keep in mind, though, that an ID should be unique to one element on
the page. Did you mean to use the ^= operator, rather than the =
operator?)

Cheers,
Andrew

On Aug 4, 9:50 pm, Valentino <sumw...@gmail.com> wrote:
> Dear all
>
> I am new to javascript and prototype. I have difficulty in writing
> function() in prototype because I am confused that function can be one
> of the parameter..i.e. some_function(abc, function())
>
> I have the following code, the two observe functions actually doing
> the same thing, only with different attributes...I don't know how to
> start to write a function for that so my code is less
> duplication...Please give me some hints on that.
>
> Thank you.
>
> <script type="text/javascript" charset="utf-8">
>     var checkboxes1 = $$("input[id=cluster_code_]");
>     var cbControl1 = $("all_cluster_selector");
>     cbControl1.observe("click", function(){
>         checkboxes1.each(function(box){
>             box.checked = cbControl1.checked;
>         });
>     });
>     var checkboxes2 = $$("input[id=hosp_code_]");
>     var cbControl2 = $("all_hospital_selector");
>     cbControl2.observe("click", function(){
>         checkboxes2.each(function(box){
>             box.checked = cbControl2.checked;
>         });
>     });
> </script>
>
> Many thanks
> Valentino
--~--~---------~--~----~------------~-------~--~----~
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 prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to