How do I trigger an event which was set up with dojo.connect?
The situation is that I have an html form with a file input field.
I want the form to be submitted asynchronously with dojo, so I connect the dojo ajax call to the onsubmit event of the form element:
dojo.connect(form_elem, 'onsubmit', function(event){dojo.xhrPost({form: form_elem});});
This (though example shown here is incomplete) works when you click the submit button - the file is submitted in the background.
Next thing is, I want to automatically submit the form when the user has selected a file, so I connect the onchange event of the file input element to the onsubmit event of the form:
dojo.connect(input_elem, 'onchange', function(event){input_elem.parentNode.onsubmit()});
(the parentNode is naturally the form element)
This doesn't work! The form element does not have an onsubmit function, even though it has the onsubmit event handler connected by dojo before.
Question is how do I trigger / fire the onsubmit event of the form?
Thanks for your help,
Jakob
NB: I am using the Dojo Toolkit v1.2
Status:
Open Jan 06, 2009 - 07:50 PM
web development, html, JavaScript, AJAX, dojo, event handling, file upload
1answer
Answer
Jan 13, 2009 - 03:24 AM
Ok, I learned something new:
The event handler and the onsubmit method are not the same.
What we need to do, instead of called the onsubmit or submit function is to fire an event using different syntaxes for Internet Explorer and Firefox.
Example:
submit_form = function(form)
{
if (document.createEvent) // Firefox etc.
{
event = document.createEvent("HTMLEvents");
event.initEvent("submit", false, true);
form.dispatchEvent(event);
}
else // IE
{
form.fireEvent("onsubmit");
}
}
Note that I am dispatching the "submit" event, not "onsubmit". When submit is about to fire, the onsubmit handler is called first and therefore the scenario still works.
All this has surprisingly little (i.e. nothing) to do with Dojo. Nevertheless I suspect Dojo has a cross browser way to do this hasslefree. I just haven't found it yet.
Thanks for you patience,
Jakob
Answer this question
Share Your Own Experience & Expertise
We look to ensure that every question is answered by the best people with relevant expertise and experience, the best answers include multiple perspectives. Do you have relevant expertise or experience to contribute your answer to any of these commonly asked questions?
Add New Comment