0

I created a text box and appended it to my div:

    $('<input />').appendTo("#box")

I would like to get the value inputted from the user.

I would also like to remove the text box too. I tried this:

    $('<input />').remove();

but no luck. Any ideas? Thanks a lot.

Greg Peckory
  • 7,700
  • 21
  • 67
  • 114

3 Answers3

3
$(function() {
    $('#box').on('change','input:text',function() {
        var userInput = $(this).val();
        $(this).remove();
    });
});
DevlshOne
  • 8,357
  • 1
  • 29
  • 37
1

You'd want to try

$("#box").find("input").remove();

And if you've got some other text boxes which you dont want to remove in #box, add an id or a class to the input when you are appending it to #box.

 $('<input />', { "class" : "remove-soon" }).appendTo("#box");

Then, you could use that class as a selector to remove it.

$("#box").find(".remove-soon").remove();

To get the value of the textbox you'd use,

$("#box").find(".remove-soon").val();
krishwader
  • 11,341
  • 1
  • 34
  • 51
1

JSbin working code http://jsbin.com/aVuVedA/1/edit

get input value

var user_value = $('#box input').val();

remove

$('#box input').remove();
Amith
  • 1,424
  • 1
  • 10
  • 22