19

I am using JQuery to dynamically (based on user choice) create tag. User enters require options in a text box and my code creates select tag of it. Script is:

var numbersString = "1,2,3,4,5,6";
var data = numbersString.split(',');

var s = $("<select id=\"selectId\" name=\"selectName\" />");
for(var val in data) {
    $("<option />", {value: val, text: data[val]}).appendTo(s);
}
s.appendTo("#msj_form");

where msj_form is my div id where the tag appends. Right now it creates:

<select id="selectId" anme="selectName">
    <option value="0">1</option>
    <option value="1">2</option>
    <option value="2">3</option>
    <option value="3">4</option>
    <option value="4">5</option>
    <option value="5">6</option>
</select>

But I also want to concatinate a Label and <tr><td> code along with tag such that the code will look like:

<tr>
    <td>My Label</td>
    <td>
        <select id="selectId" anme="selectName">
            <option value="0">1</option>
            <option value="1">2</option>
            <option value="2">3</option>
            <option value="3">4</option>
            <option value="4">5</option>
            <option value="5">6</option>
        </select>
    </td>
</tr>

enter image description here

PHP Ferrari
  • 15,754
  • 27
  • 83
  • 149
  • 5
    That looks straight forward... What have you tried? – Guffa May 14 '12 at 06:55
  • give an id to the td tag where you need to add this select tag and append to that div. – Philemon philip Kunjumon May 14 '12 at 06:59
  • I did jQuery("#msj_form").append(appendLabel+""+myelement+""); for other tags and it works fine, but it does not work for select tag. @PhilemonphilipKunjumon: actually I am creating a script by which user could create a HTML Form with his/her required fields therefore I can not hard code any td/tr tags – PHP Ferrari May 14 '12 at 07:03
  • you can do it like this ..var final=' my label here'+s+'' , then you can append to table id $('#tableid').append(final); – Tamkeen May 14 '12 at 07:05
  • @Tamkeen: I tried: var final=" my label here"+s+"";$("#msj_form").appendTo(final); but nothing displayed but when I tried: jQuery("#msj_form").append(final); browser shows: my label here [object Object] – PHP Ferrari May 14 '12 at 07:10
  • there actually is a label tag for labels :P – meo May 14 '12 at 07:17
  • #msj_form is this form id or table id ? append to the table not form var final=" my label here"+s+""; then $('#tableid').append(final); – Tamkeen May 14 '12 at 07:20
  • my friend #msj_form is DIV id – PHP Ferrari May 14 '12 at 07:25
  • I write:var ele_label = "My_Label"; var appendLabel = ""; jQuery("#msj_form").append(appendLabel+""); s.appendTo("#"+ele_label); and now it works as i want but I think is must be second option to achive my goal that is why I am still waiting for your help my friends. – PHP Ferrari May 14 '12 at 07:25
  • Another way I explore is to: Write select code in temp div then copy that code and append as I did before for all tags and filnally make empty the temp div so that temp div will behave as a fresh buffer for next select tag. my code is: s.appendTo("#tempselect"); var getselectcode = $("#tempselect").html(); $("#msj_form").append(appendLabel+""+getselectcode+""); Thanks to all my friends. Now only recommend me which solution is best and efficent? Thanks again. – PHP Ferrari May 14 '12 at 07:40

4 Answers4

53
var s = $("<select id=\"selectId\" name=\"selectName\" />");
for(var val in data) {
    $("<option />", {value: val, text: data[val]}).appendTo(s);
}

after the for loop, wrap all the content with TableRow and Cells like this , Jquery Wrap()

$(s).wrap('<table><tr><td></td></tr></table>');
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
6
<!-- Create Dropdown -->
/* 1- First get total numbers of SELECT tags used in your page to avoid elements name/id issues.
 * 2- Get all OPTIONS user entered with comma separator into a text box.
 * 3- Split user OPTIONS and make an array of these OPTIONS.
 * 4- Create SELECT code.
 * 5- Appent it into the temp div (a hidden div in your page).
 * 6- Then get that code.
 * 7- Now append code to your actual div.
 * 8- Filnally make empty the temp div therfore it will be like a new container for next SELECT tag.
 */

total = $("select").size() + 1;                                         // 1-
var myoptions = $("#myoptions").val();                                  // 2-
var data = myoptions.split(',');                                        // 3-
var s = $("<select id=\""+total+"\" name=\""+total+"\" />");            // 4-
for(var val in data) {
    $("<option />", {value: val, text: data[val]}).appendTo(s);
}
s.appendTo("#tempselect");                                              // 5-
var getselectcode = $("#tempselect").html();                            // 6-
$("#msj_form").append(appendLabel+"<td>"+getselectcode+"</td></tr>");   // 7-
$("#tempselect").html('');                                              // 8-

<div style="display:none;" id="tempselect"></div>                       // Temp div
PHP Ferrari
  • 15,754
  • 27
  • 83
  • 149
-1

Slight amendment to the answer by Ravi - appending each element one by one is a surprisingly high cost operation.

var s = $("<select id=\"selectId\" name=\"selectName\" />");
var opts = [];
for(var val in data) {
    opts.push( $("<option />", {value: val, text: data[val]}));
}

opts.appendTo(s);
vogomatix
  • 4,856
  • 2
  • 23
  • 46
  • 1) `SyntaxError: missing ) after argument list`, 2) `TypeError: opts.appendTo is not a function`. – 7stud Oct 12 '14 at 03:08
-3
var html = '<tr><td><label for="select" style="text-transform:   none;">Label_name</label></td>'
            +'<td><select name="select" id="">'
            +'<option>Choose number..</option>'
            +'<option value="0">1</option>'
            +'<option value="1>2</option>'
            +'<option value="2">3</option>'
            +'</select></td></tr>';

Let's suppose, the id of table = table_id

$('#table_id').append(html);
$('#table_id').trigger('create');

What i get is, if you don't call trigger(), the design of your select looks completely messed up..

RamGrg
  • 296
  • 1
  • 4
  • 20