<div dir="ltr">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.<br></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Fri, Mar 15, 2013 at 11:17 AM, Jeff Greenberg <span dir="ltr"><<a href="mailto:listmail.ayendesigns@gmail.com" target="_blank">listmail.ayendesigns@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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?<br>
<br>(function ($) {<br> <br> Drupal.behaviors.PlaceHolders = {<br> attach: function (context, settings) {<br><br> $(context).find('#menu-user-navigation li:first-child').prepend('<div class="settings"></div>');<br>
<br></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">On Fri, Mar 15, 2013 at 11:07 AM, Jamie Holly <span dir="ltr"><<a href="mailto:hovercrafter@earthlink.net" target="_blank">hovercrafter@earthlink.net</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
<div text="#000000" bgcolor="#FFFFFF">
<div>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:<br>
<br>
$("#x:not(.my-class-processed)", context).each(function(){<br>
$(this).addClass('my-class-processed');<br>
{..LOGIC..}<br>
})<br>
<br>
For 7:<br>
<br>
$("#x", context).once('x', function(){<br>
{..LOGIC..}<br>
<br>
})<br>
<br>
That prevents the settings div and handler from getting added
multiple time during different ajax calls that might fall within
the same context. <br>
<br>
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.<br>
<br>
var div = $('<div />').addClass('settings');<br>
$(this).prepend(div);<br>
div.toggle(<br>
// Odd clicks<br>
function(){<br>
$('#p').show();<br>
},<br>
// Even clicks<br>
function() {<br>
$('#p').hide();<br>
})<br>
<br>
Even better:<br>
<br>
var div = $('<div />').addClass('settings');<br>
$(this).prepend(div);<br>
div.click(function(){<br>
if ($('#p').is(':visible')) {<br>
$('#p').hide();<br>
} else {<br>
$('#p').show();<br>
}<br>
})<span><font color="#888888"><br>
<pre cols="72">Jamie Holly
<a href="http://www.intoxination.net" target="_blank">http://www.intoxination.net</a>
<a href="http://www.hollyit.net" target="_blank">http://www.hollyit.net</a></pre></font></span><div><div>
On 3/15/2013 10:48 AM, Jeff Greenberg wrote:<br>
</div></div></div><div><div>
<blockquote type="cite">
<div dir="ltr">
<div>I have the following in Drupal.behaviors:<br>
<br>
$(context).find('#x').prepend('<div
class="settings"></div>');<br>
$(context).find('div.settings').click(<br>
function () {<br>
$('#p').show();<br>
},<br>
function () {<br>
$('#p').hide();<br>
}<br>
);<br>
</div>
The div gets prepended, but the click does nothing. If I just
enter the jQuery('#p').show() in the console, it works fine.<br clear="all">
<div>
<div><br>
-- <br>
---
<div><a href="http://drupal.org/user/367108" target="_blank">drupal.org/user/367108</a></div>
<div><a href="http://linkedin.com/in/jeffrgreenberg" target="_blank">linkedin.com/in/jeffrgreenberg</a></div>
<div><a href="http://accidentalcoder.com" target="_blank">accidentalcoder.com</a>
/ <a href="http://ayendesigns.com" target="_blank">ayendesigns.com</a></div>
<div>@accidentalcoder</div>
</div>
</div>
</div>
<br>
<fieldset></fieldset>
<br>
</blockquote>
<br>
</div></div></div>
<br></div></div><span class="HOEnZb"><font color="#888888">--<br>
[ Drupal support list | <a href="http://lists.drupal.org/" target="_blank">http://lists.drupal.org/</a> ]<br></font></span></blockquote></div><div class="im"><br><br clear="all"><br>-- <br>---<div><a href="http://drupal.org/user/367108" target="_blank">drupal.org/user/367108</a></div>
<div><a href="http://linkedin.com/in/jeffrgreenberg" target="_blank">linkedin.com/in/jeffrgreenberg</a></div><div><a href="http://accidentalcoder.com" target="_blank">accidentalcoder.com</a> / <a href="http://ayendesigns.com" target="_blank">ayendesigns.com</a></div>
<div>@accidentalcoder</div>
</div></div>
</blockquote></div><br><br clear="all"><br>-- <br>---<div><a href="http://drupal.org/user/367108" target="_blank">drupal.org/user/367108</a></div><div><a href="http://linkedin.com/in/jeffrgreenberg" target="_blank">linkedin.com/in/jeffrgreenberg</a></div>
<div><a href="http://accidentalcoder.com" target="_blank">accidentalcoder.com</a> / <a href="http://ayendesigns.com" target="_blank">ayendesigns.com</a></div><div>@accidentalcoder</div>
</div>