c# - Dropdownlist Change event with javascript, when using an EditorTemplate -
i have editortemplate - works great. inside it, have dropdownlist decorate class, , 2 divs
<div> @html.dropdownlistfor(m => m.type, (selectlist)viewdata["typelist"], "-- select --", new { @class = "typeselector" }) @html.validationmessagefor(m => m.type) </div> <div class="emaildiv">emailstuffhere</div> <div class="calendardiv">calendarstuffhere</div>
im trying show 1 of 2 divs based on selection of dropdown list
the dropdownlist populates fine, shown dropdown list 2 items "calendar" , "email"
i have following javascript:
function checkselection() { $(".emaildiv").hide(); $(".calendardiv").hide(); var selecteditem = $(this).val(); if (selecteditem == "email") { $(".emaildiv").show(); } else if (selecteditem == "calendar") { $(".calendardiv").show(); } }; var myobject = new checkselection(); $(document).ready(function () { checkselection(); $(".typeselector").change(checkselection); checkselection.apply($(".typeselector")); });
the javascript file loads (i can see in firefox debugger) doesn't hit reason. either of divs not show.
what doing wrong?
enclose html each item in container make easier select relative elements
<div class="container"> // add or similar <div> @html.dropdownlistfor(m => m.type, (selectlist)viewdata["typelist"], "-- select --", new { @class = "typeselector" }) @html.validationmessagefor(m => m.type) </div> <div class="emaildiv">emailstuffhere</div> <div class="calendardiv">calendarstuffhere</div> </div>
and css
.emaildiv, .calendardiv { display:none; }
then script (in main view)
$(document).ready(function () { // set initial visibility $('.typeselector').each(function(index, item) { var container = $(this).closest('.container'); if ($(this).val() == 'calendar') { container.find('.calendardiv').show(); } else if ($(this).val() == 'email') { container.find('.emaildiv').show(); } }); // update visibiity on selection $('.typeselector').change(function() { var container = $(this).closest('.container'); if ($(this).val() == 'calendar') { container.find('.calendardiv').show(); container.find('.emaildiv').hide(); } else if ($(this).val() == 'email') { container.find('.emaildiv').show(); container.find('.calendardiv').hide(); } else { // may or may not necessary container.find('.emaildiv').hide(); container.find('.calendardiv').hide(); } }); }
Comments
Post a Comment