• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing A Parameter Into A Callback Function

 
Jason Ferguson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to attach a callback function into the onclick attribute of an HTML input button. The catch is, I have to pass in the value of a variable from outside the callback.

Here's the code (edited for length... might have left out a paren or semicolon):



This onclick is supposed to dynamically remove a row from a table.. the table has an ID of "itemTable", and the row has a dynamic id with "-row" appended. However, I cannot seem to successfully pass jId's value into the function... jId gets put in literally.

Since the row's ID is dynamic, I have to pass the parameter.

Help!

Jason
[ January 22, 2008: Message edited by: Eric Pascarello ]
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not have to pass it in, the closeure whould pick up the local variable. You also need to be careful. If you do not declare a variable with var it becomes global.



Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might help to clue us in on what Builder is.

In any case, you cannot control what passed to an onclick handler. In standards-compliant browsers it will be the event instance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the notation:

You are not passing jId. You are declaring that the the first parameter to the function that you are declaring will be named jId in the body of the function. This has no relation to the jId declared outside the function.

As Eric pointed out, the standard way to deal with this is to rely upon closures.
[ January 22, 2008: Message edited by: Bear Bibeault ]
 
Jason Ferguson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I think I'm looking at something akin to a scoping error. Thanks to Firebug, I know that the generated HTML for the input button looks like this:



(Note: the forum would not let me use "onclick"... its not a typo).

Maybe under "normal" circumstances, closures would take care of this, but since the button has the function inlined, it's not working correctly.

Also, Builder refers to script.aculo.us's Builder object which can automagically build DOM elements.
I don't know the best solution to my problem, unfortunately.
 
Norman Klein
author
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi J. Ferguson

It appears that you aren't grokking Laszlo correctly. Its much simpler than that. Please just put your question into very simple english and we'll provide you with an answer to your problem.

-- Norman Klein (author)
 
Jason Ferguson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ultimately solved the issue via bypassing closure issues. Here is the new Builder call:



I could probably have used some Prototype specific items to bypass document.getElementById(), but at least the thing works now.

Thanks for the help.

Jason
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Norman Klein:
Hi J. Ferguson

It appears that you aren't grokking Laszlo correctly. Its much simpler than that. Please just put your question into very simple english and we'll provide you with an answer to your problem.

-- Norman Klein (author)


Norman Klein,

This is not related to your book promotion, there are still normal questions going on in here.

Eric Pascarello
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

The problem with what you were doing in your closure was the fact that you were passing in a string instead of an object to the removeChild.

If you would change it to be ...removeChild($(jId + "-row")); it probably would work for you.

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic