Reputation: 55
I'm trying to submit a form using IEs fireEvent method. Here's some simple test code:
<html>
<head>
<script type="text/javascript">
function fireSubmit () {
if (document.createEventObject) {
var event = document.createEventObject ();
var fired = document.forms['fireForm'].fireEvent("onsubmit", event);
alert("event fired: " + fired + "; event returnValue: " + event.returnValue);
}
}
</script>
</head>
<body>
<button onmouseover="fireSubmit();">
Hover to submit
</button>
<form name="fireForm" action="action.html" method="post">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
And here's the simple action.html that should get submitted:
<html>
<body>
<script>alert('submitted');</script>
</body>
</html>
If I hover over the button, the event does get created and IE claims it was fired. An alert pops up that says "event fired: true; event returnValue: undefined", but the action.html alert is never shown and the returnValue is never set contrary to what this claims. However, if I just click on the submit button, the form is actually submitted and the "submitted" dialog shows.
What am I doing wrong? Am I misusing IE in some heinous way?
edit: Basically I'm trying to use the event model to catch problems down the line. The following code using dispatchEvent works fine in non-IE browsers for the same purpose:
if (document.createEvent) {
var event = document.createEvent("HTMLEvents");
event.initEvent("submit", false, false);
var returnValue = document.forms['fireForm'].dispatchEvent(event);
alert("event returnValue: " + returnValue);
}
Upvotes: 2
Views: 3331
Reputation: 24352
From http://www.howtocreate.co.uk/tutorials/javascript/domevents
Note that manually firing an event does not generate the default action associated with that event. For example, manually firing a focus event does not cause the element to receive focus (you must use its focus method for that), manually firing a submit event does not submit a form (use the submit method), manually firing a key event does not cause that letter to appear in a focused text input, and manually firing a click event on a link does not cause the link to be activated, etc.
The fireEvent method won't submit your form; it will trigger any event handlers attached to that event. The form's submit method will submit the form.
Is this what you want to do?
function fireSubmit () {
var fired = document.forms['fireForm'].submit();
alert("event fired: " + fired + "; event returnValue: " + event.returnValue);
}
Upvotes: 2