views:

40

answers:

1

Hi guys, ok so I have these 3 fields that I am cloning, using jQuery, but for some reason the ID's are not incrementing as they should, they all end up with the same ID's, here is the jQuery code:

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

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

        //reset the value of the text fields
        newSection.find(':text, textarea').val('');

        newSection.children(':first').children(':first').attr('id', 'gender' + newNum).attr('name', 'gender' + newNum);
        newSection.children(':nth-child(2)').children(':first').attr('id', 'age' + newNum).attr('name', 'age' + newNum);
        newSection.children(':nth-child(3)').children(':first').attr('id', 'school' + newNum).attr('name', 'school' + newNum);

        $('.clonedSection').last().append(newSection);

        $('#btnDel').attr('disabled','');

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

    $('#btnDel').click(function() {
        var num = $('.clonedSection').length; // how many "duplicatable" input fields we currently have
        $('#clonedSection' + num).remove();     // remove the last element

        // enable the "add" button
        $('#btnAdd').attr('disabled','');

        // if only one element remains, disable the "remove" button
        if (num-1 == 1)
            $('#btnDel').attr('disabled','disabled');
    });

    $('#btnDel').attr('disabled','disabled');
});

HTML:

  <div id='skewl'>
    <div id='clonedSection1' class='clonedSection'>
        <div class='clonedInput' style='width: 600px; height: 7px;'>
            <label for='gender1'>Gender: </label>
            <span class='positionMe'><select type='text' name='gender1' id='gender1' >
            <option>Select one</option>
            <option>Male</option>
            <option>Female</option>
            </select>
            </span>
        </div>
        <br />
        <div class='clonedInputTwo' style='width: 600px; height: 7px;'>
            <label for='age1'>Age: </label>
            <span class='positionMe'><input type='text' name='age1' id='age1'  /></span>
        </div>
        <br />
        <div class='clonedInputThree' style='width: 600px; height: 7px;'>
            <label for='school1'>School: </label>
            <span class='positionMe'><input type='text' name='school1' id='school1'  /></span>
        </div>
        <br />
    </div>

Any ideas?

Thanx in advance!

+1  A: 

At least part of the problem is with your use of nth-child. You have some <br> elements in there, and they are counted when you use nth-child. You'll have to adjust for those.

I do see the IDs incrementing, but they get messed up after that first <br>, so for example the "age" elements are getting the IDs for "school".

Also be aware that you're nesting the sections after it is cloned, so the second group is inside the first, the third is inside the second, and so on.

Try using .find() tag and attribute selectors to find the nested elements that you want to modify, instead of a bunch of :first and nth-child. This makes your code a little more descriptive.

For example:

 // Update gender section
newSection.find('.clonedInput > label').attr({'for':'gender' + newNum})
newSection.find('.clonedInput > span > select').attr({'id':'gender' + newNum, 'name':'gender' + newNum});

 // Update age section
newSection.find('.clonedInputTwo > label').attr({'for':'age' + newNum})
newSection.find('.clonedInputTwo > span > input').attr({'id':'age' + newNum, 'name':'age' + newNum});

 // Update school section
newSection.find('.clonedInputThree > label').attr({'for':'school' + newNum})
newSection.find('.clonedInputThree > span > input').attr({'id':'school' + newNum, 'name':'school' + newNum});

$('.clonedSection').last().after(newSection);

EDIT: Updated code to most recent version from comments below.

patrick dw
Thanx for the info, working on those changes you pointed out! Still not working though, even with that code and the breaks removed it still stays at 1! The num values seem to be incrementing, but they are not being assigned for some reason. :/
@kielie - I made a mistake. Was a little confused by the traversal. I just gave an update. I'll probably do another as I read through the code a little more.
patrick dw
@kielie - Just updated again. I didn't notice that the input was nested in a `<span>`. Should work now.
patrick dw
Yeah I noticed, not a problem, thanx for the help! Went the same way you did! Still not showing the ID's though, it seems to only be cloning the one field.
Nope, still not! I just cant see how this is not working! :/
@kielie - Here's a different example from the one above (see below). I'm going to delete that comment because I forgot to change `.append()` to `.after()`. In this example, I added a `.click()` event to each new section, so if you click an `<input>` it pops up the `HTML` so you can see that the IDs are properly set. http://jsfiddle.net/79sa2/
patrick dw
Doesn't seem to be working on jsfiddle either. . . weird, could it be my browser? I am using the latest FF, and IE8.
@kielie - I just tested in Firefox (been using Safari up til now), and it works for me. Are we talking about the same thing? I'm seeing that the `for` on the `<label>` elements and the `id` and `name` on the `:input` elements are being incremented with the `newNum`. Also the `ID` of `.clonedSection` is incremented.
patrick dw
I am seeing the .clonedSection being incremented, but the id and name on the :input elements are not, they remain the same.
@kielie - Even in the jsFiddle? That is so strange. I'm doing all my testing there, and it works for me in both browsers. When I click "add" at the top, the new section appears, and the HTML looks correct. The `<label>` elements function properly when I click on them by taking me to the proper field.
patrick dw
Wow, then I honestly have no idea, because on my browser nothing is happening, when I click add, nothing happens, let me check on my laptop quickly, maybe it's just my PC. I have jQuery 1.4.2, and I am including jquery.validationEngine.js, I am assuming that it could also be playing a factor?
@patric - nope, not working on the laptop either.
@kielie - Here's an updated example. I changed the selectors so they are more direct references `.clonedInputTwo > span > input`, I was double-wrapping the jQuery objects, so I corrected that (shouldn't matter though), and I quoted the `id:` and `name:` properties in the `.attr()` calls. Again, shouldn't matter, but I did anyway. Try it out: http://jsfiddle.net/79sa2/2/
patrick dw
...I just saw your comment above the last one. So nothing at all happens when you click `add` in the jsFiddle? Then I really have no idea what's going on.
patrick dw
Nope, nothing happens at all! And Ive tried it out on a PC and a MAC. Same here, this really is bizzare! But thank you so much for all your help!
Nope, nothing happens at all! And Ive tried it out on a PC and a MAC. Same here, this really is bizzare! But thank you so much for all your help!
@kielie - OK, but in *your* code it was at least successfully cloning the new section, right? Try to copy and paste the updated code in my answer above into your code. Be aware that when you copy code from jsFiddle, sometimes you get some weird invisible characters that cause javascript to not run. I don't think that happens when you copy from here, though. So strange that the `add` button in jsFiddle didn't do anything.
patrick dw
It seems to be yes! Ok, will do! I'll keep trying and report back! Thanx again!
Ok I just replaced the code with the code from above, and that did the trick! Thanx so much!
@kielie - You're welcome. :o)
patrick dw