0

I have below mentioned code in my JS file.

initialize: function () {

        if (!$("#Res").is(':checked'))
        {
            $("#Res").attr('checked', true);
        }
}

When I tried to run test case using Qunit.js and blanekt.js. It shows me error that "Cannot read property 'checked' of null" Can please anyone assist me to fix this issue or re-write test case. I am using Jquery 1.4.1.js version. Is issue related to version of Jquery?

test("initialize test", 1, function () {
    $('<input>').appendTo("body");
    $('<select>').appendTo("body");
    $('<input>', { type: "checkbox", checked: true, id: "Res"}).appendTo("body");
    var result = mydomain.initialize();
    equal(undefined, result, "passed");
    $('input, select').trigger("blur");    
    ok(true, "blur event called");
    $('input, select').trigger("change");
    ok(true, "change event called");
    $("input").remove();
    $("select").remove();
});
Web34
  • 131
  • 12

1 Answers1

0

Option 1.

Using your browser's dev tools, execute the following command:

$("#Res")

If you come up with null it means that your control isn't being created as you were expected.

Option 2.

If Option 1 is fine then you might be calling the initialize function before the input is created.

In any case it is difficult to help you because you haven't posted the resulting HTML by viewing the source of the page once it loads.

Option 3.

Checkbox libraries commonly use css and hide the actual checkbox input after they've initialised. You should check for available events or properties to check their state.

Ted
  • 3,985
  • 1
  • 20
  • 33