1

In the following code .tg is a class tag. I get the alert as got here but i d o not see this happen this.html(opt_arr[i]); how to resolve this issue

  function populate_combo()
  {
     var opt_arr=new Array();

     var tg_len = $('.tg').length;
     arr_len = '{{response_dict.opt_arr_len}}';
     if(arr_len == tg_len)
     {
        alert("got here")
        {% for htm in response_dict.opt_arr %}
           opt_arr.push('{{htm}}') 
        {% endfor %}
        $(".tg").each(function (i) {
        this.html(opt_arr[i]);
           });

     }
     else
     {
        alert("There was an error while loadind dropdown box data");
     }

  }

EDIT

   {% for td in response_dict.taggeddata %}
     <tr id="{{td.id}}">
     <td width="20%">{{td.field1}}</td>
      {% if response_dict.tag_flag == 1 %}
              <td class="tg"></td>
     {% endif %}
        </tr>
  {% endfor %}
Rajeev
  • 44,985
  • 76
  • 186
  • 285

2 Answers2

3

use $(this) instead of this to convert it to jquery object, and use jquery function on it..

$(this).html(opt_arr[i]);

Not Working

Working

Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
  • but actually the dropdown box doesnt appear i can see the html code – Rajeev Aug 19 '12 at 17:44
  • I presume u are trying to insert options from the `for` loop..changing html is not the way to do it.. – Rajat Singhal Aug 19 '12 at 17:47
  • but usually when u say $.html() will sonvert to html right.If not how to go about it in this case – Rajeev Aug 19 '12 at 17:50
  • give complete code..what is `.tg`..do u have select box already..btw u are removing all html of `.tg` initially..read this..http://stackoverflow.com/questions/170986/what-is-the-best-way-to-add-options-to-a-select-from-an-array-with-jquery – Rajat Singhal Aug 19 '12 at 17:53
  • I have edited the question and have given the html also have removed $('.tg').html(''); thats not required – Rajeev Aug 19 '12 at 18:02
  • Thanks got it working that was not an issue with the JS you had told – Rajeev Aug 19 '12 at 18:24
0
$(".tg").each(function (i, e) {
    e.innerHTML = opt_arr[i];
});
adeneo
  • 312,895
  • 29
  • 395
  • 388