user3817073
user3817073

Reputation:

Increment a value if ... Javascript

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);
                    }

Upvotes: 0

Views: 64

Answers (3)

Matas Vaitkevicius
Matas Vaitkevicius

Reputation: 61401

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.

Upvotes: 3

tgbrunet
tgbrunet

Reputation: 260

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);
                    }

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

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()

Upvotes: 1

Related Questions