[support] Drupal behaviors misbehaving

Jeff Greenberg listmail.ayendesigns at gmail.com
Fri Mar 15 15:22:09 UTC 2013


I should add that this was existing code, and worked until I added the
prepend and changed the click to be for the newly prepended div instead of
one that was already present.


On Fri, Mar 15, 2013 at 11:17 AM, Jeff Greenberg <
listmail.ayendesigns at gmail.com> 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>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 Hollyhttp://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
>> linkedin.com/in/jeffrgreenberg
>> accidentalcoder.com / ayendesigns.com
>> @accidentalcoder
>>
>>
>>
>>
>> --
>> [ Drupal support list | http://lists.drupal.org/ ]
>>
>
>
>
> --
> ---
> drupal.org/user/367108
> linkedin.com/in/jeffrgreenberg
> accidentalcoder.com / ayendesigns.com
> @accidentalcoder
>



-- 
---
drupal.org/user/367108
linkedin.com/in/jeffrgreenberg
accidentalcoder.com / ayendesigns.com
@accidentalcoder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20130315/2455a2ee/attachment-0001.html 


More information about the support mailing list