user1574598
user1574598

Reputation: 3881

Form validation with Jquery not working with Chrome

I've just come across a problem with my form validation for a simple checkbox in Google Chrome:

$("#index_form").submit(function(event) {

            var device = $("select[name=device]").val();
            var agree = $("input[name=agree]").checked;

            if (device==0) {
                $("#device").html(" <b style='color:red'>You must enter a device!</b>");
                event.preventDefault();
            }
            if (!agree) {
                alert("You must agree to the terms and conditions!");
                event.preventDefault();
            }

        }); 

In Chrome the alert message keeps displaying no matter if its checked or not. In Safari, its fine. I've reset Chrome and even tried it on two different computers, and the problem still persists. The checkbox is inside a table which is included with the php function. All the other validation code including the select element works well. The only way I can carry on working is to disable the line of code thats causing the problem.

Upvotes: 1

Views: 304

Answers (1)

A. Wolff
A. Wolff

Reputation: 74420

checked is a DOM element property, not jQuery one. So use instead:

var agree = $("input[name=agree]").prop('checked');

Upvotes: 3

Related Questions