Question

I am looking to write a javascript function that will fire when a user submits a form, however I do not have edit access to the submit button so that I can add the onsubmit function. I am able to add a <script> tag, so if I can detect the submit, then I can execute my code. Anyone know how to do this?

Was it helpful?

Solution

You can locate the submit button through the DOM (getElementByID() or document.formname come to mind) and then set the submit button's onsubmit value to a function of your choice.

OTHER TIPS

however I do not have programmatic access to the submit button so that I can add the onsubmit function

How is that possible? If you're executing JavaScript on page, you have access to the entire DOM.

You can use the attachEvent or addEventListener to attach an event for an DOM Object.

e.g. element = document.getElementById('submitButtonId'); element.addEventListener('click',doSomething,false);

while "doSomething" is the function name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top