Interested in new_select(..., массив) , what's wrong with the array? The function does not work with it at all. If you remove it and substitute a number for it, for example, it will work. new_select('sel1', 2)

 function new_select(id, obj) { array_s = obj; array_int = document.getElementById(id); for(i = 1; i <= array_s.length; i++) { var array_value = array_s[i][1]; var array_text = array_s[i][2]; } array_int.innerHTML = '\ <div class="select">\ <div id="t'+id+'" class="select_title">Выберите тип</div>\ <div id="s'+id+'" class="selector">\ <div class="selector_img"></div>\ </div>\ <div id="l'+id+'" class="select_list">\ ad <br />asdasd <br> asd <br />\ </div>\ </div>'; var summ_width = $('#t'+id).width() + $('#s'+id).width() + 63; $('#l'+id).css('width', summ_width); } new_select('sel1', [[0, 'Выберите тип'], [1, 'Один'], [2, 'Два']]); 
  • With an array, everything is fine, look for problems in the function. It is advisable to see the listing function at all. - stck
  • @stck, updated code. - ModaL

2 answers 2

See the exclamation marks in the code above. Incorrect indexing you get. Arrays are indexed with 0 and accordingly the last index will be array-1. And the rest is nothing supernatural. Do you use the framework? It is not clear why, at first, then you set document.getElementById (id); when you can $ ('#' + id). After all, they return the same thing - an element.

 function new_select(id, obj) { array_s = obj; array_int = document.getElementById(id); for(i = /*!*/0; i <= array_s.length-1/*!*/; i++) { var array_value = array_s[i][1]; var array_text = array_s[i][2]; } array_int.innerHTML = '\ <div class="select">\ <div id="t'+id+'" class="select_title">Выберите тип</div>\ <div id="s'+id+'" class="selector">\ <div class="selector_img"></div>\ </div>\ <div id="l'+id+'" class="select_list">\ ad <br />asdasd <br> asd <br />\ </div>\ </div>'; var summ_width = $('#t'+id).width() + $('#s'+id).width() + 63; $('#l'+id).css('width', summ_width); } 

    Problem solved. The error was in the loop. It was necessary to write this:

     i < array_s.length; 

    not like this:

     i <= array_s.length; 
    • Lol! It should be like this: for (i = 0; i <array_s.length; i ++) and not mud kneading: for (i = 1; i <array_s.length; i ++) { - Salivan
    • So you lose one item "Choose type" - stck
    • @stck, he should be lost :) - ModaL
    • one
      @Asen - var ??? no, not heard - Zowie