0

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.

4
  • Could you create a Jsfiddle,jsfiddle.net Commented Jan 27, 2014 at 9:14
  • You could create an identical input box with jQuery amd replace the original one whenever a file is uploaded. This might clear any variables the browser has set for it. Commented Jan 27, 2014 at 9:16
  • I don't understand what you are asking. You want the file field to trigger after 3 choices? Commented Jan 27, 2014 at 9:16
  • I've made a jsfiddle jsfiddle.net/2ejG4 but i noticed that it works on that!? Commented Jan 27, 2014 at 9:19

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.