Pages

I've migrated my blog

Thanks for visiting my blog. I am no longer maintaining this and I've migrated the blog to my personal domain . If you would like to follow my blog kindly use my new RSS feed

Thursday, February 10, 2011

DropDownList manipulation using jQuery

In this blog post we are going to see how can we manipulate a dropdownlist using jQuery. Here is the brief overview what we are exactly going to do.

1. Dynamically populating the dropdownlist
2. Handling the item change event of a dropdownlist
3. Getting the value and the text of the dropdownlist selected item
4. Programmatically setting the item of the dropdownlist

1. Dynamically populating the dropdownlist

jQuery Code:
$(document).ready(function () {
var myCollection = {
‘IN’: ‘India’,
‘AUS’: ‘Australia’,
‘ENG’: ‘England’
};
loadDropDownList(myCollection);
}); 

function loadDropDownList(collection) {
$.each(collection, function (index, value) {
var listItem = $(””).val(index).html(value);
$(”#countries”).append(listItem);
});

}

HTML:
Countries: 

Code Explained:

The function loadDropDownList will take a collection (a key-value pair array) and populate the dropdownlist “countries”. “$.each” is a jQuery function which iterates the array item by item and calls the “function(index,value)” for each item where index would be the item’s key and value would be the item’s value. Then the list item html markup is created for the list item and appended to the dropdownlist

2. Handling the item change event of a dropdownlist


jQueryCode:
$(document).ready(function () {
$(”#countries”).change(function () {
// Place your code here
});
}); 

Code Explained:

The jQuery API offers lot of useful functions to operate with the html form elements. One of such function is “change()” which will called whenever the item value is get changed.

3. Getting the value and the text of the dropdownlist selected item

jQueryCode:

$(document).ready(function () {
$(”#countries”).change(function () {
$(”#countryValue”).text($(”#countries”).val());
$(”#countryText”).text($(”#countries > option[selected]”).html());               
});
}); 

HTML:
Selected Country’s Text:  
Selected Country’s Value:  

Code Explained:

The jQuery code mentioned above will set the two span labels “countryValue” and “countryText” with the value and text of the selected item in the “countries” dropdownlist, when we select an item in the “countries” dropdownlist. The jQuery function “val()” enable us to find out the value of a form element, in our case we have utilized it to get the value of the “countries” dropdownlist. To get the selected item’s text we have to use the child and attribute selector together as a selector. “#countries > option[selected]” – will selects all option child elements(with the attribute selected) of item with the id “countries”. “html()” retrieves the html of the selected element. “text(value)” will set the text of the selector.

4. Programmatically setting the item of the dropdownlist

jQueryCode:
$(document).ready(function () {
$(”#btnIn”).click(function () {
$(”#countries”).val(’IN’);
});
}); 

HTML:


Code Explained:

We can programmatically select an item in the dropdownlist by setting its value.


Conclusion:

In this blog we have seen how we manipulate a dropdownlist using jQuery. You can download the source code and view the demo of this sample post here.

3 comments:

  1. To see http://www.mmomesos.com/ and world in a granda of http://www.mmohome.com/stand.And a heaven in a wild flower.Hold infinity in the palm your hand and eternity in an hour.

    ReplyDelete
  2. Delivery of the cheap Guild Wars 2 Gold via trading face to face in game.

    ReplyDelete
  3. 男士ED的“特殊反應”!
    “無性婚姻”是ED的特殊反映  前一陣子,有人公開在社會上征集“無性婚姻”;把“無性婚姻”作為一種時尚來推崇。對此,會男科學會主任委員、北大人民醫院泌尿外科主任醫師朱積川教授說,“無性婚姻”是男性性功能障礙——簡稱ED的特威而鋼 犀利士哪裡買 壯陽藥品 壯陽藥 威而鋼哪裡買 犀利士專賣 威而鋼 威而鋼哪裡買 威而鋼專賣店 威而鋼藥局 情色貼圖反映。
    多來自外地,他們在家鄉不敢看男科病,怕人知道了笑話,他們是特地利用出差、旅游的機會到其他城市來看病的。還有的病人,看病時拐彎抹角說了犀利士 犀利士 威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 犀利士 壯陽藥品去哪買 犀利士 犀利士 犀利士 犀利士 犀利士 威而鋼 犀利士半天,最后才說到實質問題——性功能障礙。有的白領夫
      “親密愛人”包括性方面的親密程度馬曉年教授說,壯陽藥 威而鋼 壯陽藥 犀利士 犀利士 犀利士專賣 犀利士哪裡買 犀利士5mg價格 壯陽藥品 犀利士專賣 威而鋼 壯陽藥 威而鋼專賣店 犀利士哪裡買就我
      馬曉年教授認為,夫妻關系長久良性的發展,壯陽藥 壯陽藥品 犀利士 威而鋼 威而鋼哪裡買 犀利士 犀利士 壯陽藥品 壯陽藥最重要的就是夫妻間的親密關系,所謂“親密愛人”,不僅僅包括夫妻之間感情上的交流程度、日常生活的親密程度,更包括性生活方面的親密程度。而很多夫妻,尤其是中年夫妻,共同生活多年,進行性生活時猶如例行公事,缺少情感交流,甚至彼此產生厭惡情緒。

    ReplyDelete