Niall Gallagher
asked on
Value from dynamically poulate Dropdownlist
I have 2 DropDown lists on a page showing departments and positions. When a user changes a department in the dropdown the other should populate with the positions available in that Dept.
I found a bit of code and altered it to do my job
I have tried $("#ddlPosition").val(posi d); but it does nothing>
also wrote a function for catching the value of the ddlPosition onChange
I found a bit of code and altered it to do my job
$("#ddlDept").change(function() {
if ($("#ddlDept").val() != "-1") {
var deptid = $(this).val();
var posid = $("#ddlPosition").val();
$.getJSON("/Details/GetPositionByDept", { selectedDeptID: deptid },
function(dropdowndata) {
var select = $("#ddlPosition");
select.empty();
select.append($('<option/>', { value: 0, text: "Select Position" }));
$.each(dropdowndata, function(index, itemData) {
select.append($('<option/>', {
value: itemData.valueOf,
text: itemData.Text
}));
and it populates great but what I want is it will select the position which has the same value as posid.I have tried $("#ddlPosition").val(posi
also wrote a function for catching the value of the ddlPosition onChange
$("#ddlPosition").change(function() {
alert($("#ddlPosition").val());
});
but it returns [object HTMLOptionElement]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Chris,
that works great for that part and Thank you
The reason I was trying to check if that works is because I want to set the selected value
Would this work then
$(":selected", "#ddlPosition").val(posid) ;
that works great for that part and Thank you
The reason I was trying to check if that works is because I want to set the selected value
Would this work then
$(":selected", "#ddlPosition").val(posid)
If you want to set the selected value of the second dropdown, just use the code you already have:
$("#ddlPosition").val(posi d);
$("#ddlPosition").val(posi
ASKER
Chris,
I tried that and it did nothing. I tried it in a few places this is the last one
I tried that and it did nothing. I tried it in a few places this is the last one
<script type="text/javascript">
$(document).ready(function() {
$("#ddlDept").change(function() {
if ($("#ddlDept").val() != "-1") {
var deptid = $(this).val();
var posid = $(":selected", "#ddlPosition").val();
$.getJSON("/Details/GetPositionByDept", { selectedDeptID: deptid },
function(dropdowndata) {
var select = $("#ddlPosition");
select.empty();
select.append($('<option/>', { value: 0, text: "Select Position" }));
$.each(dropdowndata, function(index, itemData) {
select.append($('<option/>', {
value: itemData.valueOf,
text: itemData.Text
}));
});
});
};
});
});
$("#ddlPosition").val(posid);
</script>
ASKER
Chris,
Your answer was right although I haven't got it going 100% yet but I noticed the biggest part of my problems were coming from
value: itemData.valueOf,
text: itemData.Text
When I checked my code the itemData.valueOf should have been itemData.Value
Your answer was right although I haven't got it going 100% yet but I noticed the biggest part of my problems were coming from
value: itemData.valueOf,
text: itemData.Text
When I checked my code the itemData.valueOf should have been itemData.Value
ASKER
Thanks that worked great.
OK. It needs to go after the $.each() block, but like I said - it will only work if the value still exists in the #ddlPosition dropdown after you've rebuilt it
<script type="text/javascript">
$(document).ready(function() {
$("#ddlDept").change(function() {
if ($("#ddlDept").val() != "-1") {
var deptid = $(this).val();
var posid = $(":selected", "#ddlPosition").val();
$.getJSON("/Details/GetPositionByDept", { selectedDeptID: deptid },
function(dropdowndata) {
var select = $("#ddlPosition");
select.empty();
select.append($('<option/>', { value: 0, text: "Select Position" }));
$.each(dropdowndata, function(index, itemData) {
select.append($('<option/>', {
value: itemData.valueOf,
text: itemData.Text
}));
});
$("#ddlPosition").val(posid);
});
};
});
});
</script>
Ahhh. Looks like we cross-posted :)
ASKER
By the way your last comment about moving the $("#ddlPosition").val(posi d); worked great
Thanks again
Thanks again
In the meantime, I have had good success using http://www.appelsiini.net/projects/chained for multiple selects.
The way it works is you send all of your data to the browser at once and set the class of the 2nd option group to match the value of the first. When you select the first group, the 2nd only shows classes that match the first.
Open in new window