Mr.M
Mr.M

Reputation: 1480

jquery clone not clearing the value as expected in the dynamic field

Currently working on jquery clone with my current code clone was working perfectly fine. With the clone I have added some more detailed where the user enter the number one digit number or two digit number in the Country code by default zero will added for example 12 the result will be 012 the problem here is when the user click add more button the cloning was working fine when the user enter the value in country code the value was duplicating in the cloned row actually this not happen. It tried giving clone (true,true) but still i am getting the same result.

Here is the code for dynamically adding zero

$('.cc_field').on('change',function(e){
    var len = $('.cc_field').val().length;
    if (len == 1) {
        $('.cc_field').val('00' + $('.cc_field').val());
    } else if (len == 2) {
        $('.cc_field').val('0' + $('.cc_field').val());
    } else if (len == 3) { 
    }
}); 

Here is the fiddle link for complete code

Thanks in advance

Upvotes: 2

Views: 44

Answers (1)

vijayP
vijayP

Reputation: 11502

By class .cc_field you are referring all the textboxes. Instead go for this reference to target current textfield

Updated fiddle link:

http://jsfiddle.net/vijayP/adfwtfyv/1/

$('.cc_field').on('change', function (e) {
        var len = $(this).val().length;
        if (len == 1) {
            $(this).val('00' + $(this).val());
        } else if (len == 2) {
            $(this).val('0' + $(this).val());
        } else if (len == 3) {}
});

Upvotes: 1

Related Questions