This is my javascript function which accepts id as a signature and alert the selected text of dropdown
function sendata(id) {
$(document).ready(function () {
$(id).on('change', function () {
var ctlevel = $(id).find("option:selected").text();
var selectedValue = $(id).val();
alert("Selected Text: " + ctlevel + " Value: " + selectedValue);
});
});
}
here ,i called the function but it didn't work and it did not alert the text message of dropdown also the data is not sent to controller .but when i replace the senddata function with the inner code of that function it works ??
$(document).ready(function () {
senddata('#ddtname');
$.ajax({
url:'@Url.Action("Getdata", "ConfigClass")',
type: "GET",
data: {'ctname':ctlevel},
dataType: "text",
//Response carry bunch of data from controller
success: function (response) {
alert(response);
//parse json obj to js
var obj = JSON.parse(response)
var ele = document.getElementById('sel');
$('#sel').empty();
//append default value to dropdown
var s = '<option value="-1">SubjectName</option>';
//using loop for populating dropdown ,values retrive through ajax
for (var i = 0; i < obj.length; i++) {
// POPULATE SELECT ELEMENT WITH JSON.
s+='<option value="' + obj[i]['aid'] + '">' + obj[i]['assignsubject'] + '</option>';
}
$('#sel').html(s);
},
error: function () {
alert('not working :(');
}
});
});