> I don't seem to be able to understand how to use Jquery in Tw at all...
> As a simple test I have tried to simply make a div hide when clicked.
> I have tried inline like this (With the inline JavaScript plugin installed):
> <script>
> (function($) {
> $(document).ready(function(){
> $("#note").click(function(){
> $(this).hide();
> });});
> })(jQuery);
> </script>
> <html>
> <div id="note">I am note</div>
> </html>
To set up a click event handler (as you want to do), the typical
jQuery technique is to is to use the $(document).ready(...) function
(as you have done). This is to ensure that the code is not invoked
until the target element (i.e. "#note") has already been rendered.
However, unlike a conventional web page that is rendered by the
browser as soon as it is fully loaded, the content in a tiddler is
only rendered when that tiddler is actually displayed on the page,
which occurs after the document has finished loading and the ready()
processing has already been triggered. As a result, your code for
adding a click handler to "#note" is never actually being run.
I suspect that you may have considered this and, in an attempt to work
around this 'load timing' issue, you very sensibly tried putting your
code into MarkupPreHead (or MarkupPostHead) so it could be invoked at
the end of document loading when the "ready" processing is triggered.
However... this is still a problem: even after the document has been
loaded (i.e., when ready() is triggered), the target element, "#note",
does not exist yet, because the tiddler containing it is has not been
displayed... and, of course, you cannot assign a .click(...) handler
to an element until actually exists in the document!
So... is there a way out? Yes. There is... and it's surprisingly
simple:
--------------------
<html>
<div id="note">I am note</div>
</html>
<script>
jQuery("#note").click(function(){ jQuery(this).hide(); });
</script>
--------------------
1) Put the HTML content *first*, so that the "#note" element is
created before the script is processed.
2) Simplify the script to *immediately* assign the .click() handler
(without using .ready() event trigger)
Note: to improve the readability of the above example, I used the more
verbose "jQuery(...)", rather than the shorthand "$(...)". This
avoids the need for the extra 'wrapper' syntax and turns your script
into a 'one liner' that is much easier to understand.
enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios
HELP ME TO HELP YOU - MAKE A CONTRIBUTION TO MY "TIP JAR"...
http://www.TiddlyTools.com/#Donations
Professional TiddlyWiki Consulting Services...
Analysis, Design, and Custom Solutions:
http://www.TiddlyTools.com/#Contact