JQuery function does not work in Chrome and IE

advertisements

I do not understand why this basic function does not work correctly in these browsers, when I typing in the browser console the message is 'undefined'.

    function fillYears(year) {
         var yearToSet = year - 7;
         $('#SelectYear a').each(function () {
             this.text = yearToSet;
             yearToSet++;
         });
     }

Sorry if it's something simple and I do not notice. Thank you very much.


Try:

function fillYears(year) {
     var yearToSet = year - 7;

     $('#SelectYear a').each(function () {
         $(this).text(yearToSet++);
     });
}

Or simply:

function fillYears(year) {
     var yearToSet = year - 7;

     $('#SelectYear a').text(function () {
         return yearToSet++;
     });
}

And if you want to debug your code, console.log is not the most appropriate way. In most browsers you can put a debugger; statement anywhere in your code to set a breakpoint at that location.