简体   繁体   中英

Is it possible to access the onSubmit attribute of HTML tag via JavaScript?

Let's say I have the following form:

<form id="myForm" onSubmit="myFunction()">
   ...
</form>

Can I access the value of onSubmit from JavaScript? Something like document.forms.myForm.onSubmit ?

Since onSubmit is an attribute you may use getAttribute property and thus fetch the value of the attribute.

There you go:

 alert(document.forms["myForm"].getAttribute("onSubmit")) 
 <form id="myForm" onSubmit="myFunction()"> </form> 

You could use

document.forms.myForm.onsubmit.toString()

which will return a wrapper function that contains the code in the attribute.

Yes, It's possible please refer the below code,

document.forms["formName"].onsubmit = function(){
    // process.
}

您可以使用jquery函数attr()函数(此函数可以从选定的元素获取任何属性,例如:onSubmit)

$("#myForm").attr('onSubmit')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM