Greg Peckory
Greg Peckory

Reputation: 8058

How to get value and remove text box jQuery

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.

Upvotes: 0

Views: 6188

Answers (3)

Amith
Amith

Reputation: 1434

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

get input value

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

remove

$('#box input').remove();

Upvotes: 1

DevlshOne
DevlshOne

Reputation: 8457

$(function() {
    $('#box').on('change','input:text',function() {
        var userInput = $(this).val();
        $(this).remove();
    });
});

Upvotes: 3

krishwader
krishwader

Reputation: 11371

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();

Upvotes: 1

Related Questions