Solved

Dynamically Create a select element with options using jquery

Posted on 2012-03-28
2
494 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
ID: 37779850
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
ID: 37779860
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Analytics setup 5 50
asp Google Map 2 32
Need output in powershell ( Vertical format) 2 22
On Load function does not trigger on iPhone 4 35
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

837 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