TransWikia.com

How can I pass date/text to ?

Stack Overflow Asked by Muska on December 15, 2020

I’m trying to set text in input date but nothing shows there.
Is it something wrong with data type?

<!DOCTYPE html>
<html>
<body>

<table>
    <tr>
        <td id="invoice">
        2020-09-22
        </td>
    </tr>
</table>    

<select id="decisionList">
  <option></option>
  <option>Payment date</option>
</select>

<div id="choice">
</div>



<script>

document.addEventListener("change", function () {

  var decisionList = document.getElementById("decisionList");
  var selectedOptionIndex = decisionList.options[decisionList.selectedIndex].index;  
  
  var invoiceDate = document.getElementById("invoice");
  var invoiceDateText = invoiceDate.textContent;
  
  var finalChoice = document.getElementById("choice");

  switch(selectedOptionIndex) {
    
    case 0:
        finalChoice.innerHTML='<input type="date">'
    break;
   
    case 1:
        finalChoice.innerHTML='<input type="date" value="' + invoiceDateText + '">' //here is a problem
    break;

    default:
    
    finalChoice.innerHTML=''
    
  }
  
})

</script>

</body>
</html>

I also tried to create new Date based on text I’m passing:

  var newDate = new Date(invoiceDateText);

but also doesn’t work. As you can see I’m passing 2020-09-22 (from td) so where is the problem?

2 Answers

The variable invoiceDateText contains white spaces and therefore is not parsed correctly.

You can update that part to the following.

 var invoiceDateText = invoiceDate.textContent.trim();

Correct answer by Ali Ihsan on December 15, 2020

The date should be a string and formatted by YYYY-MM-DD

Answered by triston_h on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP