So I'm kind of baffled by this problem. I'm duplicating this select when they use it. The goal is to be able more players to a team. If they add one, it creates another select. If they add a second player then a third select is created.
Currently, it's only duplicating whenever the first select is changed. Shouldn't .on()
apply to all elements created dynamically after the javascript is parsed and bound? How do I make it so it's only applying to the most recently added select
? Thanks in advance for your time!
JQuery
$(document).ready(function(){
another = $('#addplayers');
$('.addplayer').on('change', function(){
another.clone().removeAttr('id').appendTo($(this).closest('.form-horizontal'));
$(this).removeClass('addplayer');
});
});
HTML
<div class="form-group" id="addplayers">
<label class="col-sm-2 control-label">Add Player</label>
<div class="col-sm-10">
<select class="addplayer" team="<%=@team.id%>">
<%@players.each do |player|%>
<option value="<%=player.id%>"><%=player.name%></option>
<%end%>
</select>
</div>
</div>