jquery - Resetting data-Id for dynamically appended inputs -
so red squares http://imgur.com/lqyatdb showing i'm trying achieve want reset number after each champion start [1] might want add more fields first champion increase old number
$(document).ready(function(){ championnumber = 1; spellnumber=1; $('a#addchampion').on('click',function(){ $('div#championinput').append( '<div class="champion" data-id="'+championnumber+'">\ <a href="#" class="remove">remove</a>\ <br>\ <input type="text" class="championinput" list="champions" name="champion[]" placeholder="champion '+championnumber+'">\ <datalist id="champions"></datalist>\ <a href="#" class="addspell">add spell</a>\ <br>\ <div>'); for(var key in champions){ if(champions.hasownproperty(key)){ $('#champions').append('<option value="' + key + '">'); } } championnumber++; }); $('div#championinput').on('click', 'a.remove',function(){ $(this).parent('div.champion').remove(); }); $('div#championinput').on('click', 'a.addspell',function(){ $( '<div class="spell" data-id="'+spellnumber+'">\ <select name="change['+$(this).parent('div').data('id')+'][]">\ <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>\ <input type="text" name="championspell['+$(this).parent('div').data('id')+'][]">\ <br>\ <textarea type="text" size="20" name="spelldescription['+$(this).parent('div').data('id')+'][]" placeholder="enter description" />\ <select name="spellchange['+$(this).parent('.champion').data('id')+']['+spellnumber+'][]">\ <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>\ <a href="#" class="addchange">add change </a>\ <a href="#" class="removespell">remove spell</a>\ </div>\ ').appendto('.champion[data-id='+$(this).parent('div').data('id')+']'); spellnumber++; }); $('div#championinput').on('click', 'a.addchange',function(){ $(this).next('a.removespell').after( '<div class="change">\ <textarea type="text" size="20" name="spelldescription['+$(this).parent('div').data('id')+'][]" placeholder="enter description" />\ <select name="spellchange['+$(this).parent().parent('div').data('id')+']['+$(this).parent('.spell').data('id')+'][]">\ <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>\ <a href="#" class="removechange">remove change</a>\ </div>'); }); $('div#championinput').on('click', 'a.removespell',function(){ $(this).closest('.spell').remove(); }); $('div#championinput').on('click', 'a.removechange',function(){ $(this).closest('.change').remove(); }); });
maybe easiest solution update indexes after each user interaction. isn't efficient code (jquery.each
slower using for
loops), works (demo):
renumber = function(){ $('.champion').each(function( ){ var championindex = + 1; $(this) .attr( 'data-id', championindex ) .find( 'input' ) .attr( 'placeholder', 'champion ' + championindex ) .end() .find( '.spell' ) .each(function( j ){ var spellindex = j + 1; $(this) .attr( 'data-id', spellindex ) .find( 'select:first' ) .attr( 'name', 'change[' + championindex + '][' + spellindex + ']' ) .next() // input .attr( 'name', 'championspell[' + championindex + '][' + spellindex + ']' ) .next() // textarea .attr( 'name', 'spelldescription[' + championindex + '][' + spellindex + ']' ) .next() // 2nd select .attr( 'name', 'spellchange[' + championindex + '][' + spellindex + ']' ); }); }); };
call above function after item added or removed form.
Comments
Post a Comment