Forum Moderators: open

Message Too Old, No Replies

Faster way to alter submit buttons

         

TrinkDawg

4:21 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



Hi all, I have a need to find all the submit buttons on a page that is full of forms. The problem that I am having is that this is taking a long time. I am currently doing something like this:

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.

DrDoc

4:39 pm on Feb 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To better be able to answer your question -- what do you want to do with the submit button once you've found it? And, what triggers this function?

TrinkDawg

5:48 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



I'm attaching an event handler to execute a javascript function when a submit button is clicked. I do not have access to the code that generates the submit buttons, so adding an onClick to the source is (unfortunately) not an option. The thing that triggers this is the initial page load, and any subsequent loading of information from the server (I have a framework in place that will replace pieces of the page without reloading the entire page).

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.

DrDoc

7:23 pm on Feb 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can have the JS sit and "listen" for an onclick event. Then you can just use that event handler to target the button that was clicked.

DrDoc

7:41 pm on Feb 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what I mean:

<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>