wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes Passing A Parameter Into A Callback Function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Passing A Parameter Into A Callback Function" Watch "Passing A Parameter Into A Callback Function" New topic
Author

Passing A Parameter Into A Callback Function

Jason Ferguson
Ranch Hand

Joined: Aug 09, 2007
Posts: 58
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 10, 2002
Posts: 60767
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

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

Joined: Aug 09, 2007
Posts: 58
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

Joined: Jan 22, 2008
Posts: 36
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

Joined: Aug 09, 2007
Posts: 58
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing A Parameter Into A Callback Function
 
Similar Threads
Dynamic rows validation from javascript
get id of selected rows
adding and deleting a row in html
comprehension problem gwt & events
2 dimensional array