Consider following code snippet:

$(document).ready(function(){
	var f_id=1;
	$("#click1").click(function(){
		f_id++;
		$('div').append('<button id="click2">Click</button>');
	});
	$("#click2").click(function(){
		console.log(f_id);
	});
});
What will be printed to the console after clicking on button with id #click2 ?
Explanation
You can attach an event handler for dynamically added element using on() method. Example:

        var f_id=1;
        $("#click1").click(function(){
            f_id++;
            $('div').append('<button id="click2">Click</button>');
        });
        $("div").on('click','#click2',function(){
            alert(f_id);
        });

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback