Solved

Dynamically Create a select element with options using jquery

Posted on 2012-03-28
2
491 Views
Last Modified: 2012-04-05
Hi,

I want to create a select with multiple options using jquery.

<table id="tblForm"></table>

I want to append dropdown list to this table.

Here is my select list which I want to build dynamically using jquery.

<Select id="dynSelect">
 <option value="choice1">Choice1</option>
<option value="choice2">Choice2</option>
<option value="choice3">Choice3</option>
<option value="choice4">Choice4</option>
<option value="choice5">Choice5</option>
</Select>

Thanks for the help.
0
Comment
Question by:r_pat72
  • 2
2 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
For example : http://jsfiddle.net/6VZG9/1/
    $(document).ready(function() {
    
    var mySelectString = '<Select id="dynSelect"><option value="choice1">Choice1</option><option value="choice2">Choice2</option><option value="choice3">Choice3</option><option value="choice4">Choice4</option><option value="choice5">Choice5</option></Select>';

    $("#tblForm").html(mySelectString);
     
    })

Open in new window

¿
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
another one : http://jsfiddle.net/6VZG9/3/

    $(document).ready(function() {
    
        var mySelectString = '<Select id="dynSelect"></Select>';
    
        $("#tblForm").html(mySelectString);
        var j = [["choice1","Choice1"],["choice1","Choice1"],["choice1","Choice1"],["choice1","Choice1"],["choice1","Choice1"]];
        $.each(j, function(i,v) {
            $("#tblForm select").append("<option value='" + v[0] + "'>" + v[1] + "</option>");
        })
     
    })

Open in new window

¿
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now