Dynamically Adding Drop down using JQuery

No.of Views839
Bookmarked1 times
Downloads 
Votes0
By  Dhananjay Kumar   On  10 Feb 2011 07:02:33
Tag : JQuery , List Controls
In this article, we will see Creating Drop Down dynamically and Adding options to Drop down dynamically.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This article is targeted to beginners in Jquery. In this article, we will see

1.    Creating Drop Down  dynamically
2.    Adding options to Drop down dynamically.

 Image Loading

So, on $(document).ready(function() {};  , we will create  drop down and add options to that at runtime.  We are going to add two drop downs

MyPage.htm

<html>
<head>

    <script src="http://code.jquery.com/jquery-latest.min.js">
    </script>

    <script type="text/javascript">
        $(document).ready(function() {
            alert("Hello World!!!");
            var data = { 'Country': 'India', 'Country1': 'USA', 'Country2': 'Australia', 'Country3': 'Srilanka' };
            var s = $('<select />');
            for (var val in data) {
                $('<option />', { value: val, text: data[val] }).appendTo(s);
            }
            s.appendTo('body');
            var data1 = { 'City': 'New Delhi', 'City2': 'WDC', 'City3': 'Adilade', 'City4': 'Colombo' };
            var s1 = $('<select />');
            for (var val in data1) {
                $('<option />', { value: val, text: data1[val] }).appendTo(s1);
            }
            s1.appendTo('body');
        }); 
    </script>

</head>
<body>
</body>
</html>

Explanation

1.    Creating dropdown  by

Image Loading

2.    Adding option  by below script 

Image Loading

3.    Adding dropdown to body of HTML. You can add anywhere you want like div or form

Image Loading

Output

 Image Loading

 That's all hope help and thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
    In this article I will demonstration to how to validate whether to check at least one checkbox is checked in the Gridview in ASP.NET using JQuery. The JQuery is significant library to manipulate client html and css with less line of code.Before Jquery come up we have used the pure JavaScript to implement this features. I will give the JavaScript code as well in end of this article.
    Published Date : 30/Dec/2010
    Today I have implemented to my project to check and uncheck all checkboxes within gridview in asp.net. So in this article we focus how to implement this features using JQuery with less code. And also I have given live demonstration as well you can try it in live.
    Published Date : 29/Dec/2010
    In this article, I will explain how to sort custom objects array using JQuery. Let’s say you have array with custom objects, the custom object has few attributes as well, so in this case, you may need to sort objects with different order with specific attributes.
    Published Date : 12/Jan/2011
    The JQuery is powerful library to manipulate client side HTML, CSS, object and etc. On this article I will explain how to sort element within array using JQuery. In this demonstration I will show sort for string and numeric elements.
    Published Date : 11/Jan/2011
    Today I have introduced the new features for CodeGain article submit page to count characters for description textbox with JQuery on time. The character counter is support to users to display how many characters have entered and how many chars remaining in live.
    Published Date : 25/Dec/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top