You'll have to keep track of the last selected item by monitoring which one was clicked on last.
The following script should accomplish what you're looking for.
JS
$('body').append(
//Dynamic Inputs being added
$('<input />', { 'id':'test', 'type':'text'})
).append(
//Dynamic Inputs being added
$('<input />', { 'id':'test2', 'type':'text'})
).on('click', 'input[type="text"]', function(e){
//Keep track of the last selected input
$('.lastSelected').removeClass('lastSelected');
$(this).addClass('lastSelected');
});
//Get the last selected input from the page on a button click
$('#mybutton').click(function(e){
var items = $('.lastSelected')
if(items.length > 0){
alert($('.lastSelected').attr('id'));
}
});
HTML
<input id='mybutton' type='button' value='Click Me' />