SIGN UP MEMBER LOGIN:    
ARTICLE

Dynamically Adding Dropdown using JQuery

Posted by Dhananjay Kumar Articles | JQuery February 02, 2011
In this article, we will see how to create a DropDown dynamically and Add options to Drop down dynamically.
Reader Level:


Audience: Beginners

This article is targeted to beginners in Jquery.

In this article, we will see

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

image1.gif

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>
</script>
</head>
<body>
</body>
</html>

Explanation

  1. Creating dropdown by

    image2.gif

  2. Adding option by below script

    image3.gif

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

    image4.gif

Output

image5.gif

 

Login to add your contents and source code to this article
share this article :
post comment
 

dear sir if it possible to help me please help the showing dropdown list in view page that r presented in database.

Posted by chetan kumar s Jul 28, 2011

good article. Thanks

Posted by Purushottam Rathore Feb 03, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor