Search code examples
javascripthtmlfunctionreminders

Changing JavaScript content with JavaScript


I'm trying to make reminder. In HTML i'd have 2 text boxes (one for date and other for something like "Today is your grandmothers birthday!") and one button that executes the function. So when user enters date and text, and clicks button, in JavaScript function is created that will display "Today is your grandmothers birthday!" in some paragraph, when that day comes.
I know how to make function reminder ()that will display "Today is your grandmothers birthday!" on given date, but i don't know how to make function that will add my reminder() function to my JavaScript document.
Is that even possible ?


Solution

  • This is a bit silly and probably is not what you want, but you can definitely rewrite a function in JavaScript to be anything you want. Example below: first time you run the code, it will prompt you and change the function itself to do something totally different and using closure, it will capture the initial date and message to be used on the next call of itself.

    (function() {
      'use strict';
      
      function Reminder() {
     
        var aDate  = prompt('Enter date (dd/mm/yyyy): ');
        var msg = prompt('Enter message to aassociate with the date: ');
      
        var inputDate = new Date(aDate);
      
        Reminder = function()  {
            var todaysDate = new Date();
            if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
              alert(msg);
            }        
        };
     
        return Reminder;
      }
      
      //Firs time around ask for the data and dynamically change the function...
      Reminder();
      
      //call it again... this time around it should remind you and no longer pompt...
      Reminder();
      
    }());

    As the comments said, what you really want is probably to store the inputs in some sort of storage and when the page loaded, it will retrieve the data from the storage and do the calculation to whether remind the user of things that was previously saved.

    This is definitely not it, but since you asked if you can rewrite javascript from javascript, yes you can.