How can I call the remove button of temporary uploaded file programmatically using JavaScript?

enter image description here

I want to clear the form fields when the condition changes. I've done all the form fields exept the temporary uploaded file.

Can I call the remove function? If yes, Where is it?

  • Why don't you just .trigger( "click" ) on that button? This is an universal jQuery method, but I see no reason for it to fail for you.
    – Mołot
    Sep 8 '14 at 14:38

Easiest way to do is what @Molo mentioned in his comment.

$('#element_id').trigger("click"); OR $('.element_class').trigger("click");
$('#element_id').trigger("mousedown"); OR $('.element_class').trigger("mousedown");

Or you could, reset the .html() of the upload element

$('#'+id).html($('#'+id).html()); OR  $('.'+class).html($('.'+class).html());

JavaScript into Drupal can be done in various ways, you could reference your javaScript file in your theme's info file or you override the drupal_add_js function in template.php and add your JavaScript.

For more Information on how to put JavaScript into Drupal see this or this and these (module or module) could also be handy. This article explains how to programmatically submit a form in Drupal.

Here is a good tutorial, how to add your JavaScript to Drupal https://www.drupal.org/node/756722

  • You can even shorten that to $('#element_id').click();
    – Clive
    Sep 8 '14 at 15:57
  • @Clive could do, I went for trigger because its the new comer in JQuery, very very slightly faster performance wise. What I like the most about trigger is supplying the event-type as argument.
    – Raf
    Sep 8 '14 at 16:15
  • @Raf, the .trigger("click") event not working but .trigger("mousedown") works correctly.
    – Vahid
    Sep 13 '14 at 10:14
  • @JohnCroneh, glad your problem is resolved, last step, kindly mark your question as resolved.
    – Raf
    Sep 13 '14 at 20:42
  • @Raf, Sorry for the delay.
    – Vahid
    Sep 15 '14 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.