Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data using jQuery Trigger event to a Change event handler?

Is there a way to pass data to a change event via jQuery's trigger method?

The issue here is that a click event triggers the upload menu. Once the image is selected, the change event is fired, however. The data I passed into the trigger event's second parameter is passed to the click event, not to the change event. In the example below, data is undefined.

Trigger Image Change

$('.change_main_image').live('click', function() {
    $('input[name=photo].change_uploader').trigger('click', ["some string"]);
});

Event Handler

$('input[name=photo].change_uploader').live('change', function (e, data) {
   alert(data); // undefined
   //canvas resize and upload script
});
like image 240
TaylorMac Avatar asked Nov 22 '12 05:11

TaylorMac


People also ask

How do you trigger Onchange?

document. querySelector('#test'). addEventListener('change', () => console.

How do I create a trigger drop change event?

$(document). ready(function(){ $('#countrylist'). change(function(e){ // Your event handler }); // And now fire change event when the DOM is ready $('#countrylist'). trigger('change'); });


1 Answers

.live() is deprecated. Use .on() like this. And change event should be triggered, not click

$('.change_main_image').on('click', function() {
    $('input[name=photo].change_uploader').trigger('change', [{somedata:true}]);
});


$('input[name=photo].change_uploader').on('change', function (e, data) {
   alert(data.somedata); 
   //canvas resize and upload script
});
like image 163
thecodejack Avatar answered Sep 28 '22 01:09

thecodejack