1

The scenario is simple - I have some spans that when I click on them I use the event "e" to track what element the user clicked on (I have many elements like this) so getting the "e.target" is pretty simple from onClick like this:

React.createElement('span', {
    onClick: this._toggleIcons
}, span);

_toggleIcons: function(e){
    $(e.target).toggleClass("show");
},

So far so good.


However now I need to pass some additional parameters to the event "e" but I don't know how to do that. I cannot see whether this is possible/impossible.

React.createElement('span', {
    onClick: this._toggleRemoveOption( ?params? )
}, span2);

_toggleRemoveOption: function(e, param1, param2){
    $(e.target).toggleClass("remove");

    console.log(e.target);
    console.log(param1);
    console.log(param2);
},

So my question is this - how to white my onClick event handler parameters so that I can use them (param1, param2) and also have access to "e.target"?

Thank you

2
  • There is no need to add tag-words like solved, resolved, etc. to the question title, StackOverflow has a built-in way to communicate that a question has an accepted answer. :) Commented Feb 13, 2017 at 11:24
  • I see. Ok, thanks :) Commented Feb 13, 2017 at 11:25

2 Answers 2

3

Create a new function (es5ify way)

React.createElement('span', {
    onClick: (function(_this){
      return function(e) {
        _this._toggleRemoveOption(e, 'param1', 'param2')
      }
    }(this))

}, span2);

Or

React.createElement('span', {
    onClick: function(e) {
        this._toggleRemoveOption(e, 'param1', 'param2')
    }.bind(this)

}, span2);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow. :O This works like a Charm!!! Thank you my friend! I love you and stackoverflow! Thank you very much!!!
1

Use bind:

React.createElement('span', {
        onClick: this._toggleRemoveOption.bind(null, param1, param2)
    }, span2);


    _toggleRemoveOption: function(param1, param2, e){
        $(e.target).toggleClass("remove");

        console.log(e.target);
        console.log(param1);
        console.log(param2);
    },

1 Comment

This answer also works perfectly! Thank you very much!

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.