Reputation: 243
I have a text field. When user enters some text & hits enter, the text should appear below with a cross (X) mark against it. User can enter as many texts in it and hit enter and can also cancel the text using the X mark. I will use the entered text to send to the server. Can you let me know how this can be done.
Upvotes: 0
Views: 3871
Reputation: 1317
You can do like this:
HTML:
<input id="textbox" name="textbox" value="" />
<div id="textlist"></div>
Javascript
$('#textbox').keypress(function(event) {
if (event.which == '13') {
event.preventDefault();
$('#text-list').append('<div>' + $(this).val() + ' <span onclick="$(this).parent().remove();">X</span> </div>');
$(this).val('');
}
});
http://jsfiddle.net/ynhat/sKaU4/
Upvotes: 0
Reputation: 3154
Here is a quick solution for you, but it can be upgraded:
HTML
<input type="text" id="textBox" value="" />
<div id="resultsDiv"></div>
JS
$( '#textBox' ).keyup( function( eventObj ) {
if ( eventObj.which == 13 )
{
$( '<div><span>' + $( '#textBox' ).val() + '</span> <a href="javascript:;" onclick="$( this ).parent().remove();">X</a>' ).appendTo( '#resultsDiv' );
$( this ).val( '' );
}
} );
Upvotes: 1