Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery clone of a textbox without the content

Tags:

jquery

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?

like image 587
Hector Barbossa Avatar asked Dec 06 '10 11:12

Hector Barbossa


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('');
like image 106
Nick Craver Avatar answered Nov 16 '22 21:11

Nick Craver


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/

like image 24
Peter Örneholm Avatar answered Nov 16 '22 22:11

Peter Örneholm


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.

like image 1
Parth Vora Avatar answered Nov 16 '22 21:11

Parth Vora