javascript - Index-Id always has the same value for every input -


so have dynamically created form index-id of inputs names championspell[] need know 1 thought of using index-id changes every inputs index same value when pressing add spell button. can test problem here http://89.69.172.125/test.php

$(function() {               $('body').on('click', '#addspell',function() {                     $(                    '<p><select name="change[]" id="change[]" onchange="val(this)"><option value="passive">passive</option><option value="q" selected>q</option><option value="w">w</option><option value="e">e</option><option value="r">r</option></select><label for="var"><input type="text" id="championspell[]" name="championspell[]" data-id="" readonly="true"><br><textarea type="text" id="p_scnt" size="20" name="p_scnt_' + +'" value="" placeholder="enter description" /><select><option value="buff">buff</option><option value="nerf">nerf</option><option value="new">new</option><option value="change">change</option><option value="bugfix">bugfix</option></select></label> <a href="#" id="addgeneral">add change</a> <a href="#" id="remvar">remove spell</a></p>').appendto($(this).next());                      $('input[name="championspell[]"]').attr('data-id', y);                     y++;                     return false;             }); }); 

bad practice aside

(as in, using label block element)

(using jquery create such template file)

(etc)

(etc)

the problem resides variable y not being initialized, , changing data-id values in end. try updated (and bit more readable) bit

$(function () {     var y = 0;     $('#addspell').on('click', function(){         $(['<p>',                 '<select name="change[]" id="change[]" onchange="val(this)">',                     '<option value="passive">passive</option>',                     '<option value="q" selected>q</option>',                     '<option value="w">w</option>',                     '<option value="e">e</option>',                     '<option value="r">r</option>',                  '</select>',                 '<label for="var">',                     '<input type="text" id="championspell[]" name="championspell[]" data-id="' + y +'" readonly="true">',                     '<br>',                     '<textarea type="text" id="p_scnt" size="20" name="p_scnt_' + y + '" value="" placeholder="enter description">',                     '<select>','' +                         '<option value="buff">buff</option>',                         '<option value="nerf">nerf</option>',                         '<option value="new">new</option>',                         '<option value="change">change</option>',                         '<option value="bugfix">bugfix</option>',                     '</select>',                     '</label>',                 '<a href="#" id="addgeneral">add change</a>',                 '<a href="#" id="remvar">remove spell</a>',         '</p>'].join('')).appendto($(this).next());         y++;         return false;     }); }); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -