1

I am trying to add a row to a table that includes a "select2" (plugin) field. Although I get the new row the new select2 select field doesn't work. Any idea why?

p.s.: The html is in Greek, this is why I am not pasting it as well, to avoid confusion. My guess is that its a jquery issue, but anyway if you need the html let me know.

var rowtemplate = $("tr#1").html();
function createRow(num){
    var ns = "s" + num + "id_";
    var numAdd = num + 1;
    var autoGen = numAdd + 1;
    var psomi = rowtemplate.replace("_1", "_"+num);
    $("div.select2-container").remove(); // Remove all select2 elements
    return '<tr id="'+ num +'" class="troption">' + psomi + '</tr>';
}


$('a[data-role="addrow"]').click(function(event) {
    event.preventDefault();
    var lastTr =  $('tr.troption:last'),
        lastTrid = parseInt(lastTr.attr("id")),
        newid = lastTrid + 1;
        newRowHtml = createRow(newid);
        $('tr#'+lastTrid).after(newRowHtml);
        // Recreate the select2 divs
        $(".select2").select2({
                allowClear: true
            });
});

Firebug error = uncaught exception: query function not defined for Select2 s2id_autogen3

George D.
  • 1,630
  • 4
  • 23
  • 41
  • 1
    possible duplicate of [Clonned Select2 is not responding](http://stackoverflow.com/questions/17175534/clonned-select2-is-not-responding) – Michel Ayres Mar 14 '14 at 17:23

1 Answers1

-1

If you copy the code from page source code and paste your own page this error give s2id_autogen3 error.

example i copy the code from right click source this code.

Wrong copied code

<div class="form-group">
                    <label class="control-label col-md-3">Large</label>
                    <div class="col-md-4">
                        <div class="select2-container form-control input-large select2me" id="islemTipi">
                            <a href="javascript:void(0)" class="select2-choice select2-default" tabindex="-1">
                                <span class="select2-chosen" id="select2-chosen-4">Select...</span>
                                <abbr class="select2-search-choice-close"></abbr>
                                <span class="select2-arrow" role="presentation"><b role="presentation"></b></span>
                            </a><label for="s2id_autogen4" class="select2-offscreen"></label><input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-4" id="s2id_autogen4">
                        </div><select class="form-control input-large select2me select2-offscreen" data-placeholder="Select..." tabindex="-1" title="">
                            <option value=""></option>
                            <option value="AL">Alabama</option>
                            <option value="WY">Wyoming</option>
                        </select>
                        <span class="help-block"> </span>
                    </div>
                </div>

this is true and not fired select2 s2id_autogen3 not defined vs..

              <div class="form-group">
                    <label class="control-label col-md-3">Large</label>
                    <div class="col-md-4">
                        <select class="form-control input-large select2me" data-placeholder="Select...">
                            <option value=""></option>
                            <option value="AL">Alabama</option>
                            <option value="WY">Wyoming</option>
                        </select>
                        <span class="help-block">
                            .input-large
                        </span>
                    </div>
                </div>

i was solve this way.

Abdullah SARGIN
  • 1,646
  • 1
  • 13
  • 19