0

I am working on a small piece of code that looks to see if a value is equal to 'December 25 - January 9' and if it does increment the year (that I append to it) by one

However I can not figure out how to find that value. Every time I run the script it skips to the else part of the script when that value is selected. Any guidance is appreciated

if ($('#date').val === 'December 25 - January 9') {
                    var startDates = $('#date').val().split(" - ");
                    var year = $('year').val;
                    var yearDec = parseInt(year, 10) + 1;
                    var payPdStart = startDates[0] + ' ' + year;
                    var payPdEnd = startDates[1] + ' ' + yearDec;
                    var startDate = Date.parse(payPdStart);
                    myStartDates = new Date(startDate);
                    var endDate = Date.parse(payPdEnd);
                    myEndDates = new Date(endDate)
                    while (myStartDates <= myEndDates) {
                        var firstCol = "<td style='border:none;'>" + myStartDates + "</td>";
                        $('#timeTable').append("<tr>" + firstCol + "</td>");

                        var newDate = myStartDates.setDate(myStartDates.getDate() + 1);
                        start = new Date(newDate);
                    }
                }
                else{
                    var startDates = $('#date').val().split(" - ");
                    var year = $('#year').val() ;
                    var payPdStart = startDates[0] + ' ' + year;
                    var payPdEnd = startDates[1] + ' ' + year;
                    var startDate = Date.parse(payPdStart);
                    myStartDates = new Date(startDate);
                    var endDate = Date.parse(payPdEnd);
                    myEndDates = new Date(endDate)

                    console.log(myStartDates);
                    console.log(myEndDates);
                    while (myStartDates <= myEndDates) {
                        var firstCol = "<td style='border:none;'>" + myStartDates + "</td>";
                        $('#timeTable').append("<tr>" + firstCol + "</td>");

                        var newDate = myStartDates.setDate(myStartDates.getDate() + 1);
                        start = new Date(newDate);
                    }
3
  • 5
    .val() not .val - it's a function, and you have to call it. Commented Jul 9, 2014 at 15:02
  • 2
    Which you seem to do correctly several times through the rest of your code, maybe just a typo? Commented Jul 9, 2014 at 15:03
  • It's questions like this that reaffirm my belief that jQuery, as powerful a tool as it is, makes people ask the worst questions... Commented Jul 9, 2014 at 15:07

3 Answers 3

3

Problem is you are using JQuery but chexking value as in javascript

$('#date')[0].value === 'December 25 - January 9'

or

$('#date').val() === 'December 25 - January 9'

should do it.

Sign up to request clarification or add additional context in comments.

Comments

1

I see two places where you used val instead of val(), you want to call the function val not access a non-existent val field

if ($('#date').val() === 'December 25 - January 9') { // <-- val()
  var startDates = $('#date').val().split(" - ");
  var year = $('year').val(); // <-- val()

Comments

0

Try this. It should work.

You have var should be var() also year should be #year

if ($('#date').val() === 'December 25 - January 9') {
                    var startDates = $('#date').val().split(" - ");
                    var year = $('#year').val();
                    var yearDec = parseInt(year, 10) + 1;
                    var payPdStart = startDates[0] + ' ' + year;
                    var payPdEnd = startDates[1] + ' ' + yearDec;
                    var startDate = Date.parse(payPdStart);
                    console.log(startDates);
                    console.log(year);
                    console.log(yearDec);
                    console.log(payPdStart);
                    console.log(payPdEnd);
                    myStartDates = new Date(startDate);
                    var endDate = Date.parse(payPdEnd);
                    myEndDates = new Date(endDate)
                    while (myStartDates <= myEndDates) {
                        var firstCol = "<td style='border:none;'>" + myStartDates + "</td>";
                        $('#timeTable').append("<tr>" + firstCol + "</td>");

                        var newDate = myStartDates.setDate(myStartDates.getDate() + 1);
                        start = new Date(newDate);
                    }
                }
                else{
                    var startDates = $('#date').val().split(" - ");
                    var year = $('#year').val() ;
                    var payPdStart = startDates[0] + ' ' + year;
                    var payPdEnd = startDates[1] + ' ' + year;
                    var startDate = Date.parse(payPdStart);
                    myStartDates = new Date(startDate);
                    var endDate = Date.parse(payPdEnd);
                    myEndDates = new Date(endDate)

                    console.log(myStartDates);
                    console.log(myEndDates);
                    while (myStartDates <= myEndDates) {
                        var firstCol = "<td style='border:none;'>" + myStartDates + "    </td>";
                        $('#timeTable').append("<tr>" + firstCol + "</td>");

                        var newDate = myStartDates.setDate(myStartDates.getDate() + 1);
                        start = new Date(newDate);
                    }

Comments

Your Answer

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