I fill in data for a user from a JSON array. This is a simulation of exactly the problem I have.
When I click on the button number 1 it goes over the each loop 4 times. Button 2, 3 times, etc.
Is there a way to stop the $.each loop. I thought that return false;
should do this, but it doesn't seem to help. Check out the jsfiddle to see it in action.
html
<div id="add_more_asset_numbers_edit" /></div>
Jquery
var arr = [
'1',
'2',
'3',
'4'
];
$.each(arr, function (index, value) {
// alert(value);
$("#add_more_asset_numbers_edit").append("<input type='text' name='' value='"+ value+"' id='input_"+ value+"' disabled /><button type='button' id='"+ value+"_"+ value+"_1' class='btn btn-warning edit_more_assets'>Edit</button><button type='button' id='"+ value+"_"+ value+"' class='btn btn-danger remove_more_assets'>Remove</button><br id='"+ value+"_2'>");
$(".edit_more_assets").on("click", function(){
var id1 = $(this).prop("id");
alert(id1);
return false;
});
$(".remove_more_assets").on("click", function(){
var id1 = $(this).prop("id");
alert(id1)
return false;
});
});