views:

273

answers:

3
function valid()
{
    begin_checked = false;
    end_checked = false;

    alert("begin_checked: " +begin_checked);
    alert("end_checked: " +end_checked);

    if (document.dd.begin.checked.length == undefined || document.dd.end.checked.length == undefined )
    {
        alert("In undefined");
    }

    alert("end");
}

When the if statement is false, it never gets to alert("end") ? When it is true, it executes properly. Why?

+3  A: 

Are there any errors in your browsers error console? I'm guessing it's because it's trying to evaluate a property that doesn't exist, this causing it to fail (never getting to the == undefined). You can just check that the property exists or use the typeof to check if it's undefined.

if (!document.dd.begin.checked.length || !document.dd.end.checked.length)
{
    alert("In undefined");
}

if (typeof document.dd.begin.checked.length == 'undefined' || typeof document.dd.end.checked.length == 'undefined' )
{
    alert("In undefined");
}
tj111
+8  A: 

There is probably a null pointer exception and you do not have errors outputting to your browser.

You need some output to check:

alert(document);
alert(document.dd);
alert(document.dd.begin);
alert(document.dd.begin.checked);
alert(document.dd.end);
alert(document.dd.end.checked);

If you get undefined from any of those, then your code will not execute properly.

Edit: Also, the other answers here all have good information. Read those as well.

Edit2: Alternative - Surround your code in a try/catch block and alert the error:

function valid(){

    try{
        begin_checked = false;
        end_checked = false;

        alert("begin_checked: " +begin_checked);
        alert("end_checked: " +end_checked);

        if (document.dd.begin.checked.length == undefined || document.dd.end.checked.length == undefined ){
            alert("In undefined");
        }

        alert("end");

    } catch (e) {
        alert(e);
    }

}
Kevin Crowell
or you could use a try/catch block, and alert the error
rampion
That was suggested as well, but he deleted his post.
Kevin Crowell
Shouldn't that be catch (e) not catch (Exception e) ? I'm pretty sure JS doesn't have named exception types.
R. Bemrose
+2  A: 

http://getfirebug.com/

naturalethic