[support] Drupal behaviors misbehaving

Jamie Holly hovercrafter at earthlink.net
Fri Mar 15 15:25:33 UTC 2013


$('#menu-user-navigation li:first-child', context).once('my-identifier', 
function(){
   var div=$('<div />').addClass('settings');
   $(this).prepend(div);
   div.click(function(){
       if ($('#p').is(':visible')) {
             $('#p').hide();
        } else {
             $('#p').show();
        }
     })
})

Once you're inside the once callback function 'this' will reference the 
actual DOM element you are selecting work on ('#menu-user-navigation 
li:first-child'). The my-identifier can be anything. jQuery once() adds 
that to the element to indicate it has already been processed.

Jamie Holly
http://www.intoxination.net
http://www.hollyit.net

On 3/15/2013 11:17 AM, Jeff Greenberg wrote:
> Jamie, thanks for this, so with your 'even better' example, given the 
> following, with the final line being what I'll change to your line 
> beginning var div =, what would I do with the rest of the wrapping to 
> change it to the .once() methodology?
>
> (function ($) {
>
>   Drupal.behaviors.PlaceHolders = {
>     attach: function (context, settings) {
>
>       $(context).find('#menu-user-navigation 
> li:first-child').prepend('<div class="settings"></div>');
>
>
>
> On Fri, Mar 15, 2013 at 11:07 AM, Jamie Holly 
> <hovercrafter at earthlink.net <mailto:hovercrafter at earthlink.net>> wrote:
>
>     First off, you really should use one of the once methods, which is
>     different depending on if you're in Drupal 6 or 7. 6 actually
>     doesn't have that method, but rather you add a class indicating
>     it's been processed:
>
>     $("#x:not(.my-class-processed)", context).each(function(){
>       $(this).addClass('my-class-processed');
>      {..LOGIC..}
>     })
>
>     For 7:
>
>     $("#x", context).once('x', function(){
>      {..LOGIC..}
>
>     })
>
>     That prevents the settings div and handler from getting added
>     multiple time during different ajax calls that might fall within
>     the same context.
>
>     For your logic, it looks like you're confusing .click() with
>     .toggle(). Click responds to every click and only takes a single
>     callback. Toggle() takes 2 callbacks, one that is fired on odd
>     clicks and one that is fired on even.
>
>     var div = $('<div />').addClass('settings');
>     $(this).prepend(div);
>     div.toggle(
>      // Odd clicks
>       function(){
>         $('#p').show();
>       },
>       // Even clicks
>      function() {
>           $('#p').hide();
>     })
>
>     Even better:
>
>     var div = $('<div />').addClass('settings');
>     $(this).prepend(div);
>     div.click(function(){
>       if ($('#p').is(':visible')) {
>             $('#p').hide();
>        } else {
>             $('#p').show();
>        }
>     })
>
>     Jamie Holly
>     http://www.intoxination.net  
>     http://www.hollyit.net
>
>     On 3/15/2013 10:48 AM, Jeff Greenberg wrote:
>>     I have the following in Drupal.behaviors:
>>
>>     $(context).find('#x').prepend('<div class="settings"></div>');
>>           $(context).find('div.settings').click(
>>             function () {
>>               $('#p').show();
>>             },
>>             function () {
>>               $('#p').hide();
>>             }
>>           );
>>     The div gets prepended, but the click does nothing. If I just
>>     enter the jQuery('#p').show() in the console, it works fine.
>>
>>     -- 
>>     ---
>>     drupal.org/user/367108 <http://drupal.org/user/367108>
>>     linkedin.com/in/jeffrgreenberg
>>     <http://linkedin.com/in/jeffrgreenberg>
>>     accidentalcoder.com <http://accidentalcoder.com> /
>>     ayendesigns.com <http://ayendesigns.com>
>>     @accidentalcoder
>>
>>
>
>
>     --
>     [ Drupal support list | http://lists.drupal.org/ ]
>
>
>
>
> -- 
> ---
> drupal.org/user/367108 <http://drupal.org/user/367108>
> linkedin.com/in/jeffrgreenberg <http://linkedin.com/in/jeffrgreenberg>
> accidentalcoder.com <http://accidentalcoder.com> / ayendesigns.com 
> <http://ayendesigns.com>
> @accidentalcoder
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20130315/aa70a639/attachment.html 


More information about the support mailing list