Or simply:
connect('target1', 'onclick', function() {
change_html('target1', 'new html');
});
On 10/14/06, Bob Ippolito <[EMAIL PROTECTED]> wrote:
>
> On 10/13/06, Chris <[EMAIL PROTECTED]> wrote:
> >
> > Hello,
> >
> > I'm just trying to do a really simple thing by setting an event on an
> > element with connect() and then changing that element's html with
> > innerHTML. But I'm not sure how to properly pass parameters through
> > connect() to my function. I can see with MochiKit.Logging that the
> > values ARE being passed but because of the error I'm getting the
> > connect() function does not execute correctly.
> >
> > Here is my HTML:
> >
> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
> > <head>
> >
> > <title>Test 6</title>
> >
> > <script type="text/javascript"
> > src="../mochi131/lib/MochiKit/MochiKit.js"></script>
> >
> > <script>
> > function change_html (id, new_html)
> > {
> > e = MochiKit.DOM.getElement(id);
> >
> > e.innerHTML = new_html;
> >
> > MochiKit.Logging.log('i was clicked');
> > }
> >
> > function run_mystuff ()
> > {
> > ******** connect('target1', 'onclick', change_html);
> > }
> >
> > MochiKit.DOM.addLoadEvent(run_mystuff);
> > </script>
> >
> > </head>
> > <body>
> >
> > <div id="target1">
> > asdf
> > </div>
> >
> > <div id="target2">
> > </div>
> >
> > </body>
> > </html>
> >
> > -------------
> >
> > If I change the line marked with stars to...
> >
> > connect('target1', 'onclick', change_html('target1', 'new html'));
> >
> > ...I get an error that says "'objOrFunc' must be a function if
> > 'funcOrStr' is not given'.
>
> Like most languages in common use, function calls always execute
> exactly where they are in the code. What you're doing is actually
> this:
>
> var result = change_html('target1', 'new html');
> connct('target1', 'onclick', result);
>
> That clearly is not what you want.
>
> > If I hard code the values everything works fine. What do I need to
> > change?
>
> You need a function that does what it needs to do using only the event
> parameter. A typical way to do this would be to use a function that
> returns a new function with the information you need.
>
> var make_change_html = function (target, replacement) {
> return function (event) {
> event.stop();
> change_html(target, replacement);
> };
> };
>
> connect('target1', 'onclick', make_change_html('target1', 'new html'));
>
> -bob
>
> >
>
--
troels
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/mochikit
-~----------~----~----~----~------~----~------~--~---