2

I am using jquery .clone() and it is working fine. However my problem is that when I clone my input field it also clones the value of the previous field. I don't want to clone the value. How can I overcome this issue?

Here is my code

function addrow(){
    var num     = $('.clonedInput').length;
    var newNum  = new Number(num + 1);     
    var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
    $('#input' + num).after(newElem);

    jQuery('.clonedInput').each(function(){
        jQuery('#total', jQuery(this)).unbind('blur');
    });

    var ci = jQuery('.clonedInput:last');
    jQuery('#total', ci).blur(function() {
        addrow();
    });
}

Regards,

Faizan

5 Answers 5

3

try this:

var newElem = $('#input' + num).clone().attr('id', 'input' + newNum).val('');
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I have tried it but its not working. Cloning the same value again
1

I might be late with this, but it's not clear whether your "#input" is actually an input field. Only then you can set it's value.

After you place your cloned element, try:

newElem.find('input:first').val(null);

Comments

0

add this line after the clone

var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
newElem.removeAttr('value');

or

var newElem = $('#input' + num).clone().attr('id', 'input' + newNum).removeAttr('value');

2 Comments

There is no need to do this via the attribute methods since there is a shorthand notation for it.
Really interesting: removeAttr('value') worked for me but setting val("") was not changing the value of the cloned input... was going crazy with this one! :)
0

Works fine with me:

<div id="con">
<input id="input1" value="some text" />
</div>


$("#input1").clone().val("").attr("id", "input2").appendTo("#con");

http://jsfiddle.net/sXL2b/

or by using insertAfter:

http://jsfiddle.net/sXL2b/1/

Try this instead of cloning: http://jsfiddle.net/sXL2b/4/

Do you really need to clone?

6 Comments

if you check the source code of your example you'll see that although the value is not displayed the attribute is still populated with the cloned data.
@jbachman jsfiddle.net/sXL2b/2 im showing input2, i can't see it right now just because i don't have firebug on this machine. It should technically clone it. Honestly i would probably do something different than clone though.
Yes that is my problem is. I am still getting the value cloned.
@Faizan i updated with a new one without clone. Can you go without cloning?
Yes I can use other solution to.. Basically I have a row of 5 input fields and I am adding a Add new row functionality on my Form.
|
0

You can clean all the inputs with .val(""):

<div class="">        
    <div id="references">
        <div class="">
            <div class="">
                <div class="">
                    <input type="text" name="name-reference" class="" placeholder="Name" >

                </div>
                <div class="">
                    <input type="text" name="relationship-reference" class="" placeholder="Relationship" >
                </div>
            </div>
        </div>
        <div class="">
            <div class="">
                <div class="">
                    <input type="text" name="employer-reference" class="" placeholder="Employer" >
                </div>
                <div class="">
                    <input type="tel" name="phone-reference" class="" placeholder="Phone Number" >
                </div>
            </div>
        </div>
        <div class="">
            <button id="add" class="">Add</button>
            <button id="remove" style="display:none;">Remove</button>


    </div>
</div>

The script:

var reference_form_index = 0;
$("#add").on('click', function(){
   reference_form_index ++;        

    $(this).parent().parent().after($("#references").clone().attr("id","references" + reference_form_index));

    $("#references" + reference_form_index + " :input").each(function(){
        $(this).attr("name",$(this).attr("name") + reference_form_index);
        $(this).attr("id",$(this).attr("id") + reference_form_index);
        $(this).val('');
    });

$("#remove" + reference_form_index).css('display', 'inline');

$(document).on( 'click', "#remove" + reference_form_index, function(event){
        $(this).parent('div').parent('div').remove();
    } );

    $("#add" + reference_form_index).remove();

});

You can see it in action on: http://jsfiddle.net/pnovales/yF2zE/5/

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.