user1169526
user1169526

Reputation: 161

Javascript uploading same file

I have a problem on triggering the onchange event when choosing a file in a filefield which was chosen before. I need to be able to trigger the change event when uploading a file.

For example I want the following scenario to work:

step 1) User chooses test1.jpg

step 2) User clicks again and chooses test2.jpg

step 3) User clicks again and chooses test1.jpg again

In case of the last the file field doesn't trigger the onchange event any more. Is it possible to make it trigger? I've searched on Google and can't find a solution. I've tried to just replace the input field with a new one but this also doesn't work. For the last one, I did it in the following manner:

$('#picturesfile').replaceWith('<input type="file" name="filedata" id="picturesfile">');

UPDATE

I have tried it with jsfiddle and it does trigger the change event, the original problem is that the onselect event of jcrop isn't triggered I will close this question and proceed with finding a solution on that problem.

Upvotes: 0

Views: 104

Answers (1)

Alex
Alex

Reputation: 11245

Every HTML string jQuery parse and cache it in $.cache object if it less than 512 symbols. So you just needed to remove input and create it like this:

//create new input
var $newInput = $('<input>', {
    type : 'file',
    name : 'filedata',
    id : 'picturesfile'
});
//remove old and append to parentNode new
$('#picturesfile').remove().parent().append(newInput);

Upvotes: 2

Related Questions