Change Editorfor In Time Of Change Input
Solution 1:
You're trying to set the element reference itself to the value you want. Rather you need to set the value
attribute of the element reference:
document.getElementById("startDate").value = res[0];
Also, it looks like you're trying to mix server-side and client-side code here. Remember that JavaScript is not processed until the document has already been processed by the server and returned to the client. Once you're client-side, all that exists is just the DOM, which is created by the browser based on the HTML document the server sent down. In other words, the fact that you used EditorFor
or whatever is completely lost. All you have is the result (i.e. HTML) of that call to EditorFor
.
Solution 2:
You should first correct your HTML, as some attributes are not specified correctly.
<inputtype="text" value="Set Date"id="reservationtime" onchange="myFunction()">
@Html.EditorFor(model => model.StartDate, new { @class = "form-control" } ) @Html.EditorFor(model => model.EndDate, new { @class = "form-control" } )
I don't know why you are explicitly setting id for above two editors. they by default take property name as ID. so I would suggest following script for it.
<script>functionmyFunction() {
var date = document.getElementById("reservationtime").value;
var res = date.split("-");
model => model.StartDate = res[0];
if(res.length>1) {
document.getElementById("StartDate").value = res[0];
document.getElementById("EndDate").value = res[1];
// if you still want to set ID properties instead of using default use following// document.getElementById("StartDate").value = res[0];// document.getElementById("EndDate").value = res[1];
}
}
</script>
Post a Comment for "Change Editorfor In Time Of Change Input"