Required attribute HTML5

Posted by Joop on Stack Overflow See other posts from Stack Overflow or by Joop
Published on 2010-06-09T09:50:57Z Indexed on 2010/06/10 9:02 UTC
Read the original article Hit count: 158

Filed under:

First of all I will explain how I stumbled into this behavior. Within my web application I am using some custom validation for my form fields. Within the same form I have two buttons. One to actually submit the form and the other to cancel/reset the form.

Mostly I use Safari as my default browser. Now Safari 5 is out and suddenly my cancel/reset button didn't work anymore. Every time I did hit the reset button the first field in my form did get the focus. However this is the same behavior as my custom form validation. When trying it with another browser everything just worked fine. I had to be a Safari 5 problem.

I changed a bit in my Javascript code and I found out that the following line was causing the problem:

document.getElementById("somefield").required = true;

To be sure that would be really the problem I created a test scenario:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>

<body>
    <form id="someform">
        <label>Name:</label>&nbsp;<input type="text" id="name" required="true" /><br/>
        <label>Car:</label>&nbsp;<input type="text" id="car" required="true" /><br/>
        <br/>
        <input type="submit" id="btnsubmit" value="Submit!" />
    </form>
</body>
</html>

What I expected would happen did happen. The first field "name" did get the focus automatically.

Anyone else stumbled into this?

© Stack Overflow or respective owner

Related posts about html5