3

This is what i need to clone:

<p id="ex">
<label for="c2">Enter your Choice</label>
<br>
<input type="text" name="c2_r" id="c2" value="" />
<br> 
</p>

I clone it by this statement:

$("#def").append($("#ex").clone());

As i see, it is cloning the p element. But,i actually want it to clone the p tag and set the value of textbox inside it to "". Can someone explain me what to do ? Also, i need to set the id of the textbox inside cloned element to be different from the original . Please help. Thanks

1
  • 1
    Does $('#ex').clone().appendTo('#def'); work? Commented Jan 30, 2011 at 4:39

2 Answers 2

1
// first we grab the clone. We also want to change the <p>'s id, as it's
// not valid to have two elements within a page with the same ID.
$('#ex').clone().attr({
  id: 'newpid' // paragraph's ID
// next, we alter the input inside it (change ID, clear value).
}).find('input').attr({
  value: '',
  id: 'newid' // <input>'s ID
// call end to stop the ".find", then add it to the #def element
}).end().appendTo('#def');

Working Example: http://www.jsfiddle.net/F4rRQ/1/

Sign up to request clarification or add additional context in comments.

5 Comments

It doesnt work for me . What happens is , it sets the original textbox value to the clone's textbox value. It is weird. I tested it by sending the values of both the textbox to a php file and printing it there
This is happening on the jsFiddle link I provided? It's 100% working for me, and on that fiddle page. Can you maybe explain a bit more and I'll see if I can fix it up? (or make a jsfiddle of your own with what you have so I can test against it?)
Okay. I am actually adding and removing textboxes dynamically to a form. When the user clicks an add button, it will add a new textbox along with the label. If the user clicks the remove button, it should remove the textbox that was added last. The problem is when i click add, a new textbox gets added, but its value is "" when i print it in a php file. Also, when i click remove, both the original textbox and the cloned textbox get removed. I set the id value as u indicated for the cloned textbox, but still when i click remove button, both the textboxes disappear
@hari: Perhaps this updated version is what you're looking for? (I bound the actions to buttons to show the add/removal, and also used a variable to keep track)
Not a problem. Feel free to thank me by clicking the checkbox/accept next to this answer though. And welcome to StackOverflow! ;-)
0

Here you go...

var clonedP = $('#ex').clone();

clonedP
 .attr({ id: 'new_id' })
 .find('input')
  .attr({id: 'new_input_id'})
  .val('');

$('#def').append(clonedP);

I put code in place to change their id attribute, because an id must be unique per page.

You will also need to change the name attribute (i.e. different names for different inputs) if you are intending to read this data in a sane fashion.

3 Comments

Hi ... Does this work for a simple text box ?? I just have to replace 'textarea' with 'text' right?
@hari Whoops, your title threw me off. I'll amend.
Hi,it didnt work. I dont know why. Everything is right with the code. But, still both the textbox get the same value

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.