Forum Moderators: open
var inputs = document.getElementsByTagName('input');
for (var i=0; i<inputs.length; i++)
{
if ((inputs[i].type == 'submit') && (!inputs[i].doneAlready))
{
// attach an event handler here
inputs[i].doneAlready = true;
}
}
This is working fine as far as doing what I want, but the problem is that there are potentially huge forms on the page I'm working on (dynamically built forms based on tabular data with many many hidden input form elements).
My question boils down to this: Is there a faster way to get an array (or nodelist or whatever) of just the input type=submit elements, or is this looping that I am doing necessary?
Thanks.
The root of the problem is that I am intercepting the submit functionality of the forms on the page, so the name/value pair of the submit button that gets clicked is never sent to the server because I am submitting the form with JavaScript. Currently I am saving the name/value pair of the last button that gets clicked and appending that pair to the form's action so that information will get to the server.
Hope this makes sense.
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function chkbtn(e) {
if(!e) e = window.event;
//DOM
if(e.currentTarget) alert("Input type: " + e.currentTarget.type + "\nInput name: " + e.currentTarget.name + "\nInput value: " + e.currentTarget.value);
//IE
else if(e.srcElement) alert("Input type: " + e.srcElement.type + "\nInput name: " + e.srcElement.name + "\nInput value: " + e.srcElement.value);
}
document.onclick = chkbtn;
</script>
</head>
<body>
<form><input type="Button" name="foobar" value="abc"></form>
<form><input type="Button" name="barbaz" value="cde"></form>
</body>
</html>