1
$(document).ready(function() {
        $('#btnAdd').click(function() {
            var num     = $('.clonedInput').length;
            var newNum  = new Number(num + 1);

            var newElem = $('#input' + num).cloneNode(true).attr('id', 'input' + newNum);

            newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
            $('#input' + num).after(newElem);
            $('#btnDel').attr('disabled','');

            if (newNum == 5)
                $('#btnAdd').attr('disabled','disabled');
        });

I cloned the textbox but the code does not work, how do I resolve this?

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
gierg
  • 37
  • 6
  • what is the element with class 'clonedInput'? What do you need its length for? – Stefan Fandler Sep 25 '12 at 03:49
  • 1
    "Doesn't work" is the least useful description a person can use. What part of it doesn't work? What results do you get? Check your Javascript console -- are there any error messages? If so, what is the *exact* message and which line that you posted matches that message?\ – Jeremy J Starcher Sep 25 '12 at 03:56
  • People give a non-native english speaking newbie a break. Stefan: obviously to add one more than there are currently – mplungjan Sep 25 '12 at 04:04
  • @mplungjan -- Non-native to English isn't the issue. To get help in any language, the OP would have to describe the problem in more detail. – Jeremy J Starcher Sep 25 '12 at 04:18
  • Sure but op is completely new so give him time before closing. I believe i understood the issue so others might too – mplungjan Sep 25 '12 at 11:14

1 Answers1

0

Try

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

As per documentFragment.cloneNode(true) doesn't clone jQuery data

Update: perhaps you want to clone num-1 since num is the new field that does not yet exist

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236