Hi,
I am using webdropdown version: 11.2.20112.1019. I would like to retrieve value and lenght of the dropdown item(s) based upon ID and do some valildations in javascript. Can you tell me the syntax of retrieving value and length from this dropdown field.
I am getting some issues while using the below code statement:
var samplechk = $find("Webdropdown").get_currentValue();
Sometimes, I get the correct value in "samplechk" whereas sometimes, due to no reason, I am getting the below error message on the above code statement:
"Microsoft JScript runtime error: 'null' is null or not an object"
Do you know why am I getting it sometimes?
Hi SunilMehta,
Here is a code snippet showing how to set selected item:
43 //Get instance of the webdropdown
44 var dd = $find("WebDropDown1");
45 //Get item with index 0
46 var item = dd.get_items().getItem(0);
47 //Set Selected Item
48 dd.selectItem(item);
Hope that helps,
Thanks
Hi Todor,
Thanks for your reply.
May I know how to set the current value and text of a webdropdown?
Sorry, posted by accident will inlcude the second part into a second post. The following are already answered, but I will point you to the answer:
i) How to retrieve the number of elements from a webdropdown?
20 //Get instance of the webdropdown
21 var dd = $find("WebDropDown1");
22 //Get the length of the items collection
23 var length = dd.get_items().getLength();
ii) How to check if a webdropdown is filled with some value or not??
24 if (length == 0) {
25 //The WebDropDown has no value into it
26 } else {
27 //The WebDropDown has value into it
28 }
Thanks,
HI SunilMehta,
The first output is expected as selectedItem returns DropDownItem object when you alert it, you alert object.
To get 0100, or 0200 text value, you can use substring javascript functuon, which is not related to our controls. Here is an example:
30 //Get selected item
31 var selectedItem = dd.get_selectedItem();
32 //Get selected item's text
33 var selectedItemText = dd.get_selectedItem().get_text();
34 //That will take you the first four characters
35 //0100, 0200
36 alert(selectedItemText.substring(0, 4));