0

I try to insert date from one column into date field of class .choice but can't do this. When I return date field length then it shows 10 signs so it's ok. Date field also allows 10 signs to be input.

        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime? PaymentDate { get; set; }

View:

       <tr>
            <td class="payment">
                @Html.DisplayFor(modelItem => item.PaymentDate)
            </td>
            <td>
                <select id="decisionList">
                    <option value="0" selected></option>
                    <option value="1">None</option>
                </select>
            </td>

            <td class="choice"></td>

       </tr>

JS:

document.querySelectorAll("#decisionList").forEach(elem => elem.addEventListener("change", function () {

    var selectedOptionIndex = this.options[this.selectedIndex].index;
    var invoiceDateText = this.closest('tr').querySelector('.payment').textContent.trim();
    var finalChoice = this.closest('tr').querySelector('.choice');

    alert(invoiceDateText.length); //returns 10 signs so it's ok


    switch (selectedOptionIndex) {

        case 0:
            finalChoice.innerHTML = '<input type="date">'
            break;

        case 1:
            finalChoice.innerHTML = '<input type="date" value="' + invoiceDateText + '">' // and here doesn't show the date from invoiceDateText
            break;

        default:

            finalChoice.innerHTML = ''

    }}));
Muska
  • 283
  • 4
  • 15

1 Answers1

0

I found an answer to my question. The issue was that I used html helper in View and tried to insert simple input in html helper code. Crucial line to change was:

finalChoice.innerHTML = '<input class="form-control datepicker" id="item_PaymentDate" name="item.PaymentDate" type="date" value="' + invoiceDateText + '">'

You can always check how html helper code looks like when press F12 and inspect your website code

Muska
  • 283
  • 4
  • 15