0

I am getting the error below when I click the button that calls the JavaScript to do the validation. The strange thing is that everything was working before but I am not what happened now. If I select to ignore this error:

Error: '0.type' is null or not an object

then the code works fine but I get the error first then it asks me if i want to debug it, if i select No then the code works fine. Please help. thanks it seems the code stops at this line:

 if (areas[0].type == "textarea") {

but here is my entire code:

<script type ="text/javascript">
    function Validate_1() {
        var flag = false;
        var gridView = document.getElementById('<%= GridView1.ClientID %>');

        for (var i = 1; i < gridView.rows.length; i++) {
            var selects = gridView.rows[i].getElementsByTagName('select');
            //var inputs = gridView.rows[i].getElementsByTagName('input');
            var areas = gridView.rows[i].getElementsByTagName('textarea');
            if (selects != null && areas != null) {
                if (areas[0].type == "textarea") {
                    var txtval = areas[0].value;
                    var selectval = selects[0].value;
                    if (selectval == "No" && (txtval == "" || txtval == null)) {

                        flag = false;
                        break;
                    }
                    else {
                        flag = true;
                        document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'visible';
                    }
                }
            }
        }
        if (!flag) {
            alert('Please note that comments are required if you select "No" from the dropdown box.  Thanks');
            document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'hidden';
            // areas[i].focus();
            // areas.[i].style.backgroundColor = "red";
        }
        return flag;
    }
    //  document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'visible';
</script>

1 Answer 1

1
var areas = gridView.rows[i].getElementsByTagName('textarea');

getElementsByTagNane does not return null, the length would be zero

So your if check needs to change.

if (selects != null && areas != null) 

should be

if (selects.length && areas.length) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.