9

Can I clone one textbox without its content??Means if I enter some values in the textbox after cloning I want an empty textbox.Is it possible?Or jquery clone returns this as an innerHtml?

1
  • By textbox do you mean textarea? Commented Dec 6, 2010 at 12:03

3 Answers 3

19

By default, cloning copies the value with the <input> currently has, but you can just clear it when cloning, for example:

var clone = $(".myInput").clone(true).val("");

Based off your comment, you'd need something like this when cloning the row:

var newRow = $(this).closest('tr').clone(true).appendTo('table')
                                  .find('input').val('');
Sign up to request clarification or add additional context in comments.

3 Comments

Out of curiosity, what does true add, when used with clone()?
@David - it copies event handlers/data the element may have...so for example if there's a keyup handler, etc...without true none of that would copy over.
@OddOneOut, I'm sure you're welcome, but I had nothing to do with it; all the thanks to @Nick Craver =) ...and, in that vein, thanks for the explanation, Nick; I could've read the api, but...um. I was being a little lazy, maybe? =b
2

Just set the value of the cloned textbox to an empty string, like this:

HTML:

<input id="source" type="textbox" value="Some text..." />
<div id="target"></div>

JavaScript:

$(function() {
    $("#source").clone().val("").appendTo("#target");
});

Example: http://jsfiddle.net/X5x4L/

Edit: Works with textarea aswell, see: http://jsfiddle.net/X5x4L/1/

3 Comments

var newRow = $(this) .closest('tr') .clone(true) .val("") .appendTo('table');
@OddOneOut: Ok? Is that your code? Isn't it working? Just don't drop some code...
@OddOneOut: In what context? And that is not an input your are trying to clone, that's a table row.
1

In my case, I had a lot of fields in the form, so to make all them null after clonning:

$('.user-address:first').clone().appendTo('.user-addresses');
$('.user-address:last').find('[name]').val(null);

Hole helps someone.

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.