0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference

6

i have written a javascript code to compare 2dates from 2 textboxes

     function CompareDates() {
            var fdate = document.getElementById('txtFromDate');
            var edate = document.getElementById('txtToDate');
            var FromDate = fdate.value.split('/');
            var EndDate = edate.value.split('/');
            var val = 'false';

            if (parseInt(FromDate[2]) < parseInt(EndDate[2])) {
                val = 'true';
                return true;
            }
            else if (parseInt(FromDate[2]) == parseInt(EndDate[2])) {
                if (parseInt(FromDate[0]) < parseInt(EndDate[0])) {
                    val = 'true';
                    return true;
                }
                else if (parseInt(FromDate[0]) == parseInt(EndDate[0])) {
                    if (parseInt(FromDate[1]) <= parseInt(EndDate[1])) {
                        val = 'true';
                        return true;
                    }
                }
            }
            if (val == "false") {
                alert("FromDate Always Less Than ToDate");
                return false;
            }
        }

and html code is

     <asp:TextBox ID="txtFromDate" runat="server" Width="150px" />
     <ajaxToolkit:CalendarExtender CssClass="MyCalendar" runat="server"
                                   ID="ceFromDate"
                                   TargetControlID="txtFromDate"
                                   Format="dd/MM/yyyy" />
    <asp:TextBox ID="txtToDate" runat="server" Width="150px" />
    <ajaxToolkit:CalendarExtender CssClass="MyCalendar" runat="server" 
                                  ID="ceToDAte"
                                  TargetControlID="txtToDate"
                                  Format="dd/MM/yyyy" />

     <asp:Button ID="btnGenerate" runat="server" CssClass="button"
                 Text="Generate" OnClientClick="if(!CompareDates()) return false;" 
                 OnClick="btnGenerate_Click" />

the problem is that the page is running well in chrome but when i run my application in IE it throw an error

0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference

please help me to overcome from this problem.

javascript
html
asp.net
asked on Stack Overflow Jan 30, 2014 by akvickyit7 • edited Feb 7, 2014 by Amarnath Balasubramanian

4 Answers

10

The error is here:

    var fdate = document.getElementById('txtFromDate');
    var edate = document.getElementById('txtToDate');

The problem is that txtFromDate and txtToDate are the server name of controls, not the client name (look the source of page from the browser).

Try this:

    var fdate = document.getElementById('<%=txtFromDate.ClientID%>');
    var edate = document.getElementById('<%=txtToDate.ClientID%>');
answered on Stack Overflow Jan 30, 2014 by bdn02 • edited Feb 7, 2014 by Amarnath Balasubramanian
2

If your javascript is located in an external JS file (which it should), the posted solutions will not work. But you could give the text fields a unique class name and modify your code as such:

<asp:TextBox ID="txtFromDate" runat="server" Width="150px" CssClass="txtFromDate" />
<asp:TextBox ID="txtToDate" runat="server" Width="150px" CssClass="txtToDate" />

and your javascript:

var fdate = document.getElementsByClassName('txtFromDate')[0];
var edate = document.getElementsByClassName('txtToDate')[0];
answered on Stack Overflow Feb 7, 2014 by Ryan Wheale
0

Please try this

 document.getElementById('<%=txtFromDate.ClientID %>')

Change your code like following:

function CompareDates() {
    var fdate = document.getElementById('<%=txtFromDate.ClientID %>');
    var edate = document.getElementById('<%=txtFromDate.ClientID %>');
    var FromDate = fdate.value.split('/');
    var EndDate = edate.value.split('/');
    var val = 'false';

    if (parseInt(FromDate[2]) < parseInt(EndDate[2])) {
        val = 'true';
        return true;
    }
    else if (parseInt(FromDate[2]) == parseInt(EndDate[2])) {
        if (parseInt(FromDate[0]) < parseInt(EndDate[0])) {
            val = 'true';
            return true;
        }
        else if (parseInt(FromDate[0]) == parseInt(EndDate[0])) {
            if (parseInt(FromDate[1]) <= parseInt(EndDate[1])) {
                val = 'true';
                return true;
            }
        }
    }
    if (val == "false") {
        alert("FromDate Always Less Than ToDate");
        return false;
    }
}
answered on Stack Overflow Jan 30, 2014 by Afnan Ahmad
0

You are missing the # for your id reference.
Change from:

var fdate = document.getElementById('txtFromDate'); 

to:

var fdate = document.getElementById('#txtFromDate');

I was wrong about the above statements.
I was able to fix the same error in my jquery code where I was missing the #. getElementById does not need this special character.

answered on Stack Overflow Aug 4, 2016 by user6678799 • edited Aug 4, 2016 by skypjack

User contributions licensed under CC BY-SA 3.0