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?
3 Answers
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('');
3 Comments
David Thomas
Out of curiosity, what does
true add, when used with clone()?Nick Craver
@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.David Thomas
@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
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
Hector Barbossa
var newRow = $(this) .closest('tr') .clone(true) .val("") .appendTo('table');
Peter Örneholm
@OddOneOut: Ok? Is that your code? Isn't it working? Just don't drop some code...
Peter Örneholm
@OddOneOut: In what context? And that is not an input your are trying to clone, that's a table row.