0

I want to pass an object called hotTraitement in parameters of an event .click of jquery. So I declare my object like this :

$(window).load(function(){
        container = document.getElementById('tab_traitement');
        var hotTraitement = new Handsontable(container, {
            data: data_traitement});
});

And I'm trying to pass this object like this :

$('#submit_button_traitement').click(function({hotTraitement:hotTraitement})
{
    console.log(hotTraitement);
});

But it's not working, hotTraitement is undefined.

Help please !

Erlaunis
  • 1,433
  • 6
  • 32
  • 50

3 Answers3

1

Try this. Define the hotTraitement variable as global one.

$('#submit_button_traitement').click({hotTraitement:hotTraitement},function(e){
   console.log(e.data.hotTraitement);
});

SIMPLE DEMO: FIDDLE

Update:

Try this way.

$(document).ready(function() {
  var container = document.getElementById('tab_traitement');
  var hotTraitement = new Handsontable(container, {
    data: data_traitement
  });

  //...

  $('#submit_button_traitement').click({
    hotTraitement: hotTraitement
  }, function(e) {
    console.log(e.data.hotTraitement);
  });

});
John R
  • 2,741
  • 2
  • 13
  • 32
  • It stays undefined :/ – Erlaunis Aug 19 '15 at 15:40
  • Did you define this `hotTraitement` variable globally? – John R Aug 19 '15 at 15:43
  • I think. I'm outside the window.load. – Erlaunis Aug 19 '15 at 15:44
  • In your fiddle, you use a simple string, and I'm using an object. Maybe it's different ? – Erlaunis Aug 19 '15 at 15:47
  • It didn't work with my code but I tried to do it in an other page, with the same code but simplified and it worked. So I used your method on the real code and it didn't work. So I had something in the options of the declaration of my handsontable (hotTraitement) which blocked what I want. I modify what was wrong, and now, every things work fine! Thanks to you ! (2 weeks to finally find a solution, you're my hero ! ^^) – Erlaunis Aug 20 '15 at 08:01
0

Try substituting $(document).ready() for $(window).load() ; utilizing .on() , data property of event

$(document).ready([
  function() {
    // define `hotTraitement`
    hotTraitement = {
      "abc": 123
    };
  }, 
  function() {
    // set `event.data` to `hotTraitement`
    $("#submit_button_traitement")
    .on("click", hotTraitement, function(event) {
        // do stuff with `event.data` : `hotTraitement`
        console.log(event.data)
    })
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="submit_button_traitement">click</div>
guest271314
  • 1
  • 15
  • 104
  • 177
0

why you want to pass hotTraitement parameter in onclick function instead you can simply access this inside your onclick even like this

$(window).load(function(){
    container = document.getElementById('tab_traitement');
    var hotTraitement = new Handsontable(container, {
        data: data_traitement});

   $('#submit_button_traitement').click(function(){
      console.log(hotTraitement);
   });
});
Zebi Rajpot
  • 186
  • 5
  • But it doesn't work. I get an object but I can't apply the method that I want. – Erlaunis Aug 19 '15 at 15:23
  • then simply in your way don't create hotTraitement object inside window load method just simple add this lines outside window load function this will work for you .... – Zebi Rajpot Aug 19 '15 at 15:31
  • how ? because in JSFIDDLE it is working ... just create your hotTraitement object before click event and outside of everything – Zebi Rajpot Aug 19 '15 at 15:38