Crystal Rouse
asked on
JavaScript that runs when the View loads.
I have Javascript functions to set Hidden Form fields when a drop-down list changes and this works correctly. Now I need to set those fields when the page loads as well.
My View:
I have the followoing JavaScript Class. I can't get this to work although I thought using init would set the values when the page loads?
My View:
<script src="~/Content/classes/Report.js"></script>
<script type="text/javascript">
var Report = new ReportClass();
Report.init();
</script>
<div class="row">
@using (Html.BeginForm("Report", "Location", FormMethod.Post))
{
<div class="col-xs-3">
@Html.DropDownList("System", sl.getSystemList(), new
{
@type = "drop",
@id = "System",
@onchange= "setReportFilter()"
})
</div>
<div class="col-xs-2 ">
<button type="submit" class="btn btn-info">
Filter
</button>
</div>
}
<div class="col-xs-2 col-xs-offset-3">
@using (Html.BeginForm("ExportReport", "Location", FormMethod.Post, new
{
@target = "_blank",
}))
{
@Html.Hidden("ReportFilter")
<button id="exportBtn" type="submit" class="btn btn-info" style="float:right">
Export To Excel
</button>
}
</div>
</div>
<script type="text/javascript">
function setReportFilter() {
var sys = document.getElementById("System");
var sysSelection = sys.options[sys.selectedIndex].text;
document.getElementById("ReportFilter").value = sysSelection;
}
</script>
I have the followoing JavaScript Class. I can't get this to work although I thought using init would set the values when the page loads?
function ReportClass() {
this.system;
this.systemFilter;
var sys = document.getElementById("System");
var sysSelection = sys.options[sys.selectedIndex].text;
document.getElementById("ReportFilter").value = sysSelection;
}
ReportClass.prototype = {
//Maps variables to the correct field
init: function () {
var sys = document.getElementById("System");
var sysSelection = sys.options[sys.selectedIndex].text;
document.getElementById("ReportFilter").value = sysSelection;
}
};
appendPrototype(ReportClass);
ASKER
I'm not getting any alerts. Really strange.
no error in the debugger console?
ASKER
Actually, yes. Cannot read property 'options' of null at line 8:
var sysSelection = sys.options[sys.selectedIn dex].text;
Thanks!
var sysSelection = sys.options[sys.selectedIn
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
JFYI, you can choose answer(s) that helped you resolve your issue
confirm you egt three alert, check the field is found and the value set
Open in new window