Event handlers are bound to existing elements. Your id="btn" element doesn't exist at the time you try to bind the handler.
But you can use an delegate event handler. I.e. you bind the handler to an already existing element and let it "filter out" events for descendants (which are possibly added later).
$("#txt").on( "click", "#btn", function() {
alert($("#btn").val());
});
Since you're binding the event based on an id and therefore only one such element can exist, this technique might not seem too useful. But it works the same as if you were binding it to a class of elements, e.g.
<!DOCTYPE html >
<html>
<head>
<title>...</title>
</head>
<body>
<div id="buttondiv">
</div>
<button id="addbutton"></button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
(function() {
var btncnt = 0;
// only binding the event handler once
// on the existing div wrapping the buttons
// and "waiting" for click-events on descandant button.somebutton elements
$("#buttondiv").on("click", "button.somebutton", function() {
alert( $(this).val() );
});
// and it will fire regardless
// of how many such buttons are added by this:
$("#addbutton").click( function() {
var btn = $("<button></button>");
btn.val(++btncnt);
btn.addClass("somebutton");
btn.append("click me");
$("#buttondiv").append(btn);
});
$("#addbutton").append("add button");
})();
</script>
</body>
</html>