How to set `onSubmit` dynamically with JavaScript

pattyd picture pattyd · Jun 15, 2013 · Viewed 8.1k times · Source

Right now I am in a bit of a dilemma because I cannot edit the onSubmit of my form, so I will have to set it dynamically. My problem is, I have no idea how to do this without overwriting the value that is already in the onSubmit
For example, lets say I have this code:

<form name="bob" onSubmit="something-i-cannot-change">

</form> 

How would I set another function to happen onSubmit along with the other function that is already in there? I looked this up and had no luck... Thanks, I really appreciate any help!!!

Note: If this question is a duplicate of anything, then please let me know!

Answer

Felipe picture Felipe · Aug 8, 2015
document.getElementById("<element-id>").setAttribute("onSubmit", "<action>");