micah, thanks for you contribution, but I smell much better solution
here.

Thanks for any quick hint.

On Aug 29, 11:38 pm, micah <[EMAIL PROTECTED]> wrote:
> a common trick i use to get around situations where an event may be
> bound twice is to add an unbind into the chain. like so:
>
> var kd = function(event) {
>     alert(event.keyCode);
>
> }
>
> $().unbind('keydown', kd).keydown(kd);
>
> it's a bit quick and dirty, but it works.
>
> -micah
>
> On Aug 29, 9:12 am, me-and-jQuery <[EMAIL PROTECTED]> wrote:
>
> > Hello. So I have a problem that when I get jQuery code in ajax
> > response and you click few times to activate ajax call, there are
> > multiple instances of same code. Lets see the example with keydown.
>
> > FILE 1:
> > <script>
> > $(document).ready(
> >         function() {
> >                         $("#my_div").click(function() {
> >                                         $.ajax({
> >                                                         type: "GET",
> >                                                         url: "test.php",
> >                                                         success: 
> > function(data) {
> >                                                                         
> > $("#div_ajax").html(data);
> >                                                         }
> >                                         })
> >                         })
> >         }
> > )
> > </script>
> > <div id="my_div">Click here</div>
> > <div id="div_ajax"></div>
>
> > FILE 2 - AJAX RESPONSE:
> > <script>
> > $().keydown(function(event) {
> >                 alert(event.keyCode);});
>
> > </script>
> > Ajax content loaded...
>
> > So, if I click on my_div twice, there will be two listeners for keys
> > and for same keydown two alerts.
>
> > I can solve this issue with ifs, but what is the best way to prevent
> > this behaviour? Maybe in the way of singletons or something?
>
> > Thanks for any advice, I am sure there must be simple and effective
> > solution. Have a nice day.

Reply via email to