最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If the type attribute in <button> tag is missing then will that button created with <button&am

programmeradmin0浏览0评论

Suppose, I have following HTML code :

<button>Click Me!</button>

In above code I have not used the attribute 'type'.

So, in this case will this button behave like a Submit Button which will submit all the form data to the server?

If yes, then can I execute JavaScript code on the onclick event of above button which is missing the 'type' attribute?

If no, what exactly will happen?

Thanks.

Suppose, I have following HTML code :

<button>Click Me!</button>

In above code I have not used the attribute 'type'.

So, in this case will this button behave like a Submit Button which will submit all the form data to the server?

If yes, then can I execute JavaScript code on the onclick event of above button which is missing the 'type' attribute?

If no, what exactly will happen?

Thanks.

Share Improve this question edited Nov 27, 2016 at 11:15 PHPLover asked Nov 27, 2016 at 11:09 PHPLoverPHPLover 13k53 gold badges172 silver badges324 bronze badges 1
  • 2 why was this down voted this is a legitimate question – Buddhi741 Commented Nov 27, 2016 at 11:16
Add a ment  | 

2 Answers 2

Reset to default 4

If you want a button to behave like just a button, then yes, your markup needs to be:

<button type="button">Click Me!</button>

In this case, you can easily add a click event with:

document.getElementsByTagName('button')[0].addEventListener('click',myFunction,false);

N.B. If you don't mark up the button properly (ie. you omit the type attribute) and the browser then treats it like a input type="submit", you can still add a submit event:

document.getElementsByTagName('button')[0].addEventListener('submit',myFunction,false);

Yes, the default type for button is submit; as is documented in the spec.

So, in this case will this button behave like a Submit Button which will submit all the form data to the server?

Yes, it will.

If yes, then can I execute JavaScript code on the onclick event of above button which is missing the 'type' attribute?

Yes, of course you can, just like any other button. The click handler will run before the form is submitted.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论