jQuery and ASP.NET

March 31, 2009

Add TextBox value to DropDownList or Html SELECT using jQuery




You have a dropdownlist or an HTML Select with you. You now want to add additional options to the SELECT which you enter in the textbox, on the click of a button.
Here's how to do so using jQuery. The same technique can also be used for an ASP.NET DropDownList without causing a postback.


<html xmlns="http://www.w3.org/1999/xhtml">


<head runat="server">


<title></title>


<script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>


<script type="text/javascript">


    $(document).ready(function() {


        $("#Button1").click(function() {


            $('#Select1').append($("<option>" + $('#Text1').val() + "</option>"));


            return false;


        });


    });    


</script>


</head>


<body>


<form id="form1" runat="server">


<div>


    <select id="Select1" name="Select1">


        <option>Option1</option>


        <option>Option2</option>


        <option>Option3</option>


        <option>Option4</option>


    </select>


    <input id="Text1" type="text" />


    <input id="Button1" type="submit" value="button" />


</div>


 


</form>


</body>


</html>




'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Add TextBox value to DropDownList or Html SELECT using jQuery"
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions