0
<div style="visibility:hidden;"><input type="file" name="photo" id="photo" /></div>

$('#photo').trigger('change');
$('#photo').change(function(e) {
  alert("change fun");
});

But alert is not displaying i mean change is not triggering at all. My question is how to trigger change from jquery without actually browsing through file $().trigger('change') is proper right?

SSS
  • 1,380
  • 3
  • 28
  • 48
  • event in your case is `change` not `click`, so trigger `change` instead. – Jai Oct 23 '13 at 05:01
  • check it once http://stackoverflow.com/questions/2721250/jquery-change-method-on-input-type-file – PSR Oct 23 '13 at 05:02
  • better your try the bind function for change http://api.jquery.com/bind/ – mohan Oct 23 '13 at 05:02
  • put your jquery code in $(document).ready(e){ //yourcode } function – Sohil Desai Oct 23 '13 at 05:10
  • @Jai , yeah i tried with change trigger. But not working . Change function is not triggering – SSS Oct 23 '13 at 05:10
  • @SSS i have posted an answer i hope thats what you want, but yet you have to put correct event to trigger, that is `change` in your case. – Jai Oct 23 '13 at 05:37

3 Answers3

1

Try this:

 $(document).ready(function(){
    // if you want file change event try this
    $('#photo').on('change', function(e){
         alert('change function');
    });
    // Or if you want click event try this
    $('#photo').on('click', function(e){
         alert('click function');
    });
 });

Here is the Demo

super
  • 2,288
  • 2
  • 21
  • 23
1

See you have to stack the event properly, what i mean is :

  1. First you create your event which is change in your case
  2. Then after it trigger the event which will work

follow like below:

$('#photo').change(function(e) { // change event created
  alert("change fun");
});

$('#photo').trigger('change'); // then trigger

Although you can bind multiple events with the use of .on() method:

$('#photo').on('change click', function(e) { // change event created
  alert("change fun");
});

$('#photo').trigger('change'); // Here you can trigger either "change" or "click"

Note:

You were triggering the wrong event and also at wrong place. You need to apply an event first only then you can trigger that event.

Jai
  • 74,255
  • 12
  • 74
  • 103
0

DEMO

Use Trigger event inside the change function

$('#photo').change(function(e) {
$(this).trigger('click');
  alert("change fun");
});
underscore
  • 6,495
  • 6
  • 39
  • 78