I’m working on an interactive web form that does not use an UpdatePanel, so I’m trying to use JavaScript to do most of the functionality. For this question, I’m trying to figure out how to get java script to add functions to a dropdownlist on PageLoad().
I have the following ASP file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Default.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
Discovery Form Templates
<asp:DropDownList ID="uiFormTemplates" runat="server" DataTextField="Subject" DataValueField="DiscoveryFormID" AppendDataBoundItems="true" OnChange="GetTemplateValue();">
<asp:ListItem Text="--Select One--" Value=""/>
</asp:DropDownList>
</div>
<div id="ChangePlate"></div>
</form>
</body>
</html>
And then this javascript:
function GetTemplateValue()
{
var dropdown = document.getElementById("uiFormTemplates");
var SelectedOption = dropdown.options[dropdown.selectedIndex].value;
if (SelectedOption == null) {
document.getElementById("ChangePlate").innerText = "There is nothing here.";
}
else {
document.getElementById("ChangePlate").innerText = dropdown;
}
}
I’m trying to use the following javascript:
$(document).ready(function () {
$("#uiFormTemplates").onchange(function () { GetTemplateValue(); });
});
When I remove OnChange="GetTemplateValue()" from the dropdownlist, nothing happens, even with the second javascript method. Did I write my code wrong, or am I not even approaching this from the right angle? Either code critiques or some direction would be helpful right now, I’m a js noob.
Assuming you’re including jQuery (which you’re using), there’s no
onchangemethod. You have to change it toon('change', ...), or use thechangemethod. Also,#uiFormTemplatesshouldn’t work, you have to use your control’sClientIDSo:
Or: