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.

3

3 Answers 3

3
$(function() {
    $('#box').on('change','input:text',function() {
        var userInput = $(this).val();
        $(this).remove();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

get input value

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

remove

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.