I have a button that adds a new div with a select tag, every option has custom attributes. my problem is when I create a several divs the function only refers to the last div I have created when I am trying to change the first select tag the function not responding.
I have tried to give all of the divs and the input a different id's but that doesn't work.
<button type="button" class="cam_field_button btn waves-effect waves-light btn-outline-info" >הוסף צלם</button>
var max_fields = 25; //maximum input boxes allowed
var wrapper = $(".camera_men"); //Fields wrapper
var add_button = $(".cam_field_button"); //Add button ID
var x = 0; //initlal text box count
$(add_button).click(function (e) { //on add input button click
e.preventDefault();
if (x < max_fields) { //max input box allowed
x++; //text box increment
$(wrapper).append('<div> <br> <select name="cam[]" id="cam_select'+x+'" class="form-control" >\n' +
' <option email="email" phone="phone" value="צלם 1">צלם 1</option>\n' +
' <option email="email2" phone="phone2" value="צלם 2">צלם 2</option>\n' +
' <option email="email3" phone="phone3" value="צלם 3">צלם 3</option>\n' +
' </select><a href="#" class="remove_field">הסר צלם ' + x + '</a><div> <input type="hidden" name="cam_email[]" id="cm_email' + x + '">\n' +
' <input type="hidden" name="cam_phone[]" id="cm_phone' + x + '">'); //add input box
$('#cam_select'+x).on('change',function () {
var email = $('#cam_select'+x+' option:selected').attr("email");
var phone = $('#cam_select'+x+' option:selected').attr("phone");
$('#cm_email'+x).val(email);
$('#cm_phone'+x).val(phone);
})
}