Setting date input value with moment not working

4281 views javascript
6

I have a modal with a date input that's appended to the body. I then set the value of the input using moment.js and proper format, from my understanding its year-month-day. Yet the value is empty. Is this because the input is being created via js? or is the format I'm using with moment wrong? Thanks in advance.

$("body").append("<div class='overlay'></div>");
$("body").append("<div class='modal'></div>");
$(".modal").append("<input id='dateJump' type='date' >");
$("#dateJump").value = moment().format('YYYY-MM-DD');
body {
height: 600px;
}
.modal {
    width:80%;
    height: 40px;
    z-index: 10;
    position: fixed;
    top: 50%;
    left:50%;
    transform: translate(-50%, -50%);
    background-color:white;
}
.overlay {
    width:100%;
    height: 100%;
    z-index: 9;
    position: fixed;
    top: 0;
    left:0;
    background-color: rgba(0,0,0,0.5);
}
.modal input {
    -webkit-appearance: none;
    border-radius:none;
    display:block;
    width: 100%;
    height: 40px;
}
input[type="date"],
select:focus {
  font-size: 16px;
  border: none; 
  border-radius: none;
  padding: 1em;
  vertical-align: center;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<body>

</body>

answered question

1 Answer

1

This works for me

$(document).ready(()=>{
    $("body").append("<div class='overlay'></div>");
    $("body").append("<div class='modal'></div>");
    $(".modal").append("<input id='dateJump' type='date' >");
    $("#dateJump").val(moment().format('YYYY-MM-DD'))   

})

posted this

Have an answer?

JD

Please login first before posting an answer.