0

I know there are a LOT of similar question to this, but I've tried them all to no avail...so thanks for any help.

WHAT I AM TRYING TO DO: I want to pass one-to-many parameters into each checkboxes 'click' event upon registration (see below).

WHAT WORKS: I can register the event WITHOUT any parameters, and the click event raises...but I need to pass-in a reference to the containing JavaScript grid object (for other reasons).

WHAT FAILS I've tried various forms of "this.MutuallyExclusiveCheckBoxHandler = function(grid){}" to no avail.

ONE IDEA: I "think" currying may be the answer, but I don't quite know how to do it well-enough (yet).

This area instantiates the grid and registers the checkboxes

<script type="text/javascript">
<!--
    // CLASS
    function CommitImporterGrid() {
        // PROPERTIES
        this.Toaster = new Toaster();
        this.CheckBoxes = new Array();

        // METHODS
        this.RegisterMutuallyExclusiveCheckBox = function(clientId) {

            var checkBox = $j('input:checkbox#' + clientId);

            // HERE: "I need to pass a reference to the grid somehow"
            $j(checkBox).click(this.MutuallyExclusiveCheckBoxHandler);

            this.CheckBoxes.push(checkBox); // Append to array
        }
        this.MutuallyExclusiveCheckBoxHandler = function() {

            // The checkbox events break when I try to add a parameter.
            var myGrid = "I need to retreive the grid reference here somehow";

            if (!$j(this).is(':checked')) { // They clicked on the same checkbox
                this.checked = true;
                return;
            }

            // ...other code...
        }
    }

     // CLASS INSTANCE
     var myGrid = new CommitImporterGrid();

     // DOM EVENT: Document.Ready()
     $j(document).ready(function() {

         // DYNAMIC REGISTRATION
         myGrid.RegisterMutuallyExclusiveCheckBox('chkCommitImporter01');
         myGrid.RegisterMutuallyExclusiveCheckBox('chkCommitImporter02');
     });
-->
</script>

2 Answers 2

1

Use .bind() and pass event data (assuming your grid is the current object):

// HERE: "I need to pass a reference to the grid somehow"
checkBox.bind('click', {grid: this}, this.MutuallyExclusiveCheckBoxHandler);

(you don't need to pass checkbox to jQuery, it is already a jQuery object)

and change your method to:

 this.MutuallyExclusiveCheckBoxHandler = function(event) {
      var mygrid = event.data.grid;
      //...
 }

(You can can access the event data via event.data)

Sign up to request clarification or add additional context in comments.

4 Comments

@SLaks: Why? It should, otherwise, this.MutuallyExclusiveCheckBoxHandler won't work either.
Inside the handler, this will be the DOM element.
@SLaks: I think inside the handler, this should refer to the DOM element, otherwise, !$j(this).is(':checked') would not make sense.
Felix is correct: within the handler "this" did indeed referred to the (checkbox) element. So the "is checked" logic is working. As for the even.data solution...it works like a CHAMP. Thanks!
0

You need to add an anonymous function that calls your handler with your parameters, like this:

this.RegisterMutuallyExclusiveCheckBox = function(clientId) {
    var self = this;

    var checkBox = $j('input:checkbox#' + clientId);

    $j(checkBox).click(function() {
        self.MutuallyExclusiveCheckBoxHandler(parameters);
    });

    this.CheckBoxes.push(checkBox); // Append to array
};

Inside the event handler, this refers to the element that triggered the event.
I therefore use a separate self variable instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.