Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Submit buttons in Mozilla

Status
Not open for further replies.

RicksAtWork

Programmer
Nov 1, 2005
120
GB


I have a form with various fields and two submit buttons.

I have set the default event handler for the submit event to point to a validation method i.e.

document.getElementById("Form1").onsubmit=Validate;

In my function Validate, I want to determine which button was pressed is this possible?



 
Client-side, you'll have to attach an onclick event to both buttons, setting a variable to some different value, then testing for that value in the Validate function.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You can add a property value to submit button. But you need onclick handler on the submit buttons to do it consistently. (If you use onclick handler to submit button, event.srcElement would be available in ie. I guess that's why you ask specifically for moz.)

I set "fired" boolean property.
[tt]
<html>
<head>
<script language="javascript">
function checkit(obj) {
var csubmitbtn=document.forms[obj.name].sbmt
for (var i=0; i<csubmitbtn.length;i++) {
if (csubmitbtn.fired) {
alert("submit button value submitted: " + csubmitbtn.value)
}
}
//continue with other validation to determine the return
//I use return false to stop submit for testing
return false;
}
function setfired(obj) {
var csubmitbtn=document.forms[obj.form.name].elements[obj.name];
for (var i=0; i<csubmitbtn.length;i++) {
csubmitbtn.fired=false;
}
obj.fired=true;
}
</script>
</head>
<body>
<form name="form1" onsubmit="return checkit(this)">
<input type="text" name="txt11" value="txt11" />
<input type="submit" name="sbmt" value="submit1" onclick="setfired(this)" />
<input type="submit" name="sbmt" value="submit2" onclick="setfired(this)" />
<input type="submit" name="sbmt" value="submit3" onclick="setfired(this)" />
<input type="submit" name="sbmt" value="submit4" onclick="setfired(this)" />
</form>
</body>
</html>
[/tt]
If you use script to assign submit handler, it would be something like this.
[tt]
window.document.form1.onsubmit=function() {
checkit(document.forms["form1"]);
}
[/tt]
which corresponds to your Validate.
 
tsuji said:
If you use onclick handler to submit button, event.srcElement would be available in ie. I guess that's why you ask specifically for moz.

Moz does have it's equivalent to srcElement (called target), but in either case, the onsubmit is attached to the form, and so this is passed as the srcElement or target, not the button that was clicked.

Perhaps you might be able to achieve something with event bubbling, but the easiest way is to attach onclick (and an onkeypress which checks for chr(13) - people could still submit the form using the enter key) to each submit button.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
I wouldn't disagree, manarth. Only wonder why moz is highlighted by the op as if solution is known to him for ie.
 
Solved it with this - totally unobtrusive!

document.getElementById('Form1').onsubmit=ValidationKernal;
var buttonClicked = null;

//Add Click events to all submit buttons
var controls = document.getElementById("Form1");


for (i=0; i<controls.length;i++)
{

if(controls.type=="submit")
{

addEvent(controls, 'click', DefaultClick);

}

}

}

function ValidationKernal()
{
originalObject = buttonClicked;

var btnUpdate = document.getElementById('TimeSheetGridView_btnUpdate');

var btnSubmitTimeSheet = document.getElementById('btnSubmitTimeSheet');

if(btnUpdate!=null && originalObject.id==btnUpdate.id)
{
return CheckTimeSheetTotals();
}

else if (btnSubmitTimeSheet!=null && originalObject.id==btnSubmitTimeSheet.id)
{
return confirm('Submit your timesheet?');
}

return true;

}

function DefaultClick(e)
{

eventObject = (e)? e:event;

//IE
originalObject = e.srcElement;

//Mozilla
if(originalObject == null)
{
originalObject = e.target;
}

buttonClicked = originalObject;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top