I’m having an issue with a DropDownList multiselect (Reference).
What I want…
I want to programmatically click some checkboxes from the dropdown using their values (for postback matters).
BRIEFING
In ‘Reference’, I found out that there’s a bug in jQuery, so I needed to do this ->
$("select").multiselect("widget").find(":checkbox").each(function(){
this.click();
});
So, I tried this and worked… then, I added the “[value=’value’]”, and it worked. That’s exactly what I needed.
$("select").multiselect("widget").find(":checkbox[value='value']").each(function(){
this.click();
});
Here’s what I did: I obtained an array with the values of the selected before postback (consultantValues). Then, I want to find the checkbox on the dropdown “Consultant” with a value from the array, and finally, click it.
Code->
for (var x = 0; x < consultantValues.length; x++) {
var consultantSelected = ":checkbox[value='" + consultantValues[x] + "']";
var doConsultantStringCode = $("id$='ddlConsultant']").multiselect("widget").find(consultantSelected);
for (var a = 0; a < doConsultantStringCode.length; a++) {
doConsultantStringCode[a].click();
}
}
This works like a charm :B
By the way, I use the “for” instead of the “each” because it runs faster.
PROBLEM
In other dropdown, I try to do the same… it worked, but suddenly it stopped working :/
Code ->
for (var i = 0; i < selectedManagements.length; i++) {
var checkboxValue = ":checkbox[value='" + selectedManagements[i] + "']";
var findCheckbox = $("[id$='ddlManagement']").multiselect("widget").find(checkboxValue);
for (var k = 0; k < findCheckbox.length; k++) {
findCheckbox[k].click();
}
}
I’m stuck with this :/
NOTES:
- What I was able to see, the findCheckbox has length = 0… don’t understand why now has 0 length when before had 1…
- the dropdowns are ASP controls.
- If you need any extra info, just let me know
Instead of emulating clicks, why don’t you just add
checked="checked"attribute? This is actually way too hack-ish to trigger DOM events that user did not initiate.Just do this: