Forum Moderators: open
JSP code:
<div class="ico">
<a href="#" title="modify" onclick="submitForm()"> <img src="../images/ico_natiz.gif" alt="modifica" border="0"> </a>
</div>
Javascript code:
function submitForm(nameId){
if(nameId){
document.getElementById(nameId).onsubmit();
document.getElementById(nameId).submit();
}
else{ document.getElementById("submitForm").onsubmit(); document.getElementById("submitForm").submit();
}
}
Thanks for the help in advance:)
The function "submitForm(nameId)" requires something to be passed to it.
Also are you checking if a certain id exists?
Then instead of
if(nameId){
it should be something like
if(document.getElementById(nameId) != null)
When the 2nd suggestion for checking the id "if(document.getElementById(nameId) != null)" is implemented, it works in firefox but not in IE.
Is there any other changes that can be done to get it working in IE too? I have been struggling to get ride of this bug for over a week now.
As for you function, it's attempting to take a string input and get the form element with that ID. If no string was input, it defaults to getting a form with the id of "submitForm". You can reduce the number of calls to document.getElementById to make it more efficient. Also, the call to the form's onsubmit() handler will cause an error if that handler has not been set. So that could be rewritten:
function submitForm(formId) {
// Get the form element using the passed in formId
// or the default 'submitForm' if no id passed in.
var frm = document.getElementById(formId ? formId: 'submitForm');
// Perform a check to make sure the form exists
if (frm != null) {
// Perform a check to make sure the form's
// onsubmit event handler was set
if (frm.onsubmit != null) {
frm.onsubmit();
}
frm.submit();
}
}
Note, this is very much dependent on event 'handlers' (vs. event 'listeners').