0

I have checked out this thread and this one but it didn't really help me.

I have a ASP:CheckBox control shown below.

<asp:CheckBox ID="chbArchived" runat="server" Checked='<%# Bind("archived") %>' OnClick=changeExpirationDate() />

My JavaScript are follows:

<script type="text/javascript">
        $(document).ready(function () {
            //alert("got here");
            $('.insDateTime').datetimepicker({
                ampm: true
            });
            changeExpirationDate(){
                var currentDate = new Date;
                var futureDateTime = (currentDate.getMonth() + 4) + '/' + currentDate.getDate() + '/' + currentDate.getFullYear() + ' ' + formatAMPM(currentDate);
                var expiredDateTime = (currentDate.getMonth() + 1) + '/' + currentDate.getDate() + '/' + currentDate.getFullYear() + ' ' + formatAMPM(currentDate);
                var archivedCheckbox = document.getElementById('cphContent_fmvNewsEventList_chbArchived').checked;
                if(archivedCheckbox == true)
                {
                    document.getElementById('cphContent_fmvNewsEventList_txtExpirationDate').value = expiredDateTime;
                }
                else{
                    document.getElementById('cphContent_fmvNewsEventList_txtExpirationDate').value = futureDateTime;
                }
            };
            function formatAMPM(date) {
                var hours = date.getHours();
                var minutes = date.getMinutes();
                var ampm = hours >= 12 ? 'pm' : 'am';
                hours = hours % 12;
                hours = hours ? hours : 12; // the hour '0' should be '12'
                minutes = minutes < 10 ? '0'+minutes : minutes;
                var strTime = hours + ':' + minutes + ' ' + ampm;
                return strTime;
            };
        });        
    </script>

The problem is I kept getting this JavaScript error "Uncaught ReferenceError: changeExpirationDate is not defined" when I clicked on the checkbox. Any suggestion/help is much appreciated.

Community
  • 1
  • 1
2myCharlie
  • 1,587
  • 2
  • 21
  • 35

3 Answers3

1
});
changeExpirationDate(){    <-- where is the function declaration?
    var currentDate = new Date;

aka

function changeExpirationDate () {
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

Change

changeExpirationDate(){

to

function changeExpirationDate(){

The first makes it look like you are trying to call it; the second defines it.

Matt
  • 20,108
  • 1
  • 57
  • 70
0

Okay, after looking at one more time, I found out it was a very stupid mistake. I forgot the "function" before the function name, changeExpirationDate(), thus, it was giving me this error.

2myCharlie
  • 1,587
  • 2
  • 21
  • 35
  • Sure, but it'll still fail if you declare it inside the `.ready()` callback like that. A handler assigned as an HTML attribute executes in the global scope. – cookie monster Jan 16 '14 at 19:43