0

I need to send parameters to the on change event of a dropdown which is generated on runtime. How can i accomplish such thing like:

$(options.host_element).find('select').on('change',(graph_widget.draw_graph).bind(options));

options is the parameter i want to send to the function draw_graph, the function is linked but not the parameters

Umes Bastola
  • 527
  • 2
  • 6
  • 18
  • 1
    [`.on( events [, selector ] [, data ], handler )`](http://api.jquery.com/on/) - check `data` parameter – code-jaff Jun 15 '15 at 05:48

2 Answers2

2

Not sure of your function's signature, but you can pass parameters to the function itself just like you do to any normal function, so it would look something like this:

$(options.host_element).find('select').on('change',function(){
    graph_widget.draw_graph(options);
});

Bind is used to change the value of this that will be used in the function, not quite what you're looking for.

I made a small fiddle to demonstrate:

Fiddle

Omri Aharon
  • 16,959
  • 5
  • 40
  • 58
1

jQuery.on has an optional parameter called data:

$(options.host_element).find('select').on('change', options, graph_widget.draw_graph);

Later in your event handler, you may access data using e.data:

function draw_graph(e) {
   var options = e.data;
}
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206