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

Suppose, I have following HTML code :<button>Click Me!<button>In above code I have not use

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744743520a4591169.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信