Solved

Dynamically Create a select element with options using jquery

Posted on 2012-03-28
2
493 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 34
flex div overflows its container 4 22
Select case on click 3 16
Else condition 9 19
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

809 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