0

I am trying to use the same form twice in the same page the form is working once but not twice i think the problem is with id or something but i am not sure i am not very good with javascript

Help appreciated.

<!DOCTYPE html>
<html>
<head>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


    <script>




    $(function () {
    $('#btnAdd').click(function () {
        var num     = $('.clonedInput').length, // Checks to see how many "duplicatable" input fields we currently have
            newNum  = new Number(num + 1),      // The numeric ID of the new input field being added, increasing by 1 each time
            newElem = $('#entry' + num).clone().attr('id', 'entry' + newNum).fadeIn('slow'); // create the new element via clone(), and manipulate it's ID using newNum value


        // H2 - section
        newElem.find('.heading-reference').attr('id', 'ID' + newNum + '_reference').attr('name', 'ID' + newNum + '_reference').html('Entry #' + newNum);

        // Title - select
        newElem.find('.label_ttl').attr('for', 'ID' + newNum + '_title');
        newElem.find('.select_ttl').attr('id', 'ID' + newNum + '_title').attr('name', 'ID' + newNum + '_title').val('');

        // First name - text
        newElem.find('.label_fn').attr('for', 'ID' + newNum + '_first_name');
        newElem.find('.input_fn').attr('id', 'ID' + newNum + '_first_name').attr('name', 'ID' + newNum + '_first_name').val('');




    // Insert the new element after the last "duplicatable" input field
        $('#entry' + num).after(newElem);
        $('#ID' + newNum + '_title').focus();

    // Enable the "remove" button. This only shows once you have a duplicated section.
        $('#btnDel').attr('disabled', false);

    // Right now you can only add 4 sections, for a total of 5. Change '5' below to the max number of sections you want to allow.
        if (newNum == 5)
        $('#btnAdd').attr('disabled', true).prop('value', "You've reached the limit"); // value here updates the text in the 'add' button when the limit is reached 
    });

    $('#btnDel').click(function () {
    // Confirmation dialog box. Works on all desktop browsers and iPhone.
        if (confirm("Are you sure you wish to remove this section? This cannot be undone."))
            {
                var num = $('.clonedInput').length;
                // how many "duplicatable" input fields we currently have
                $('#entry' + num).slideUp('slow', function () {$(this).remove();
                // if only one element remains, disable the "remove" button
                    if (num -1 === 1)
                $('#btnDel').attr('disabled', true);
                // enable the "add" button
                $('#btnAdd').attr('disabled', false).prop('value', "add section");});
            }
        return false; // Removes the last section you added
    });
    // Enable the "add" button
    $('#btnAdd').attr('disabled', false);
    // Disable the "remove" button
    $('#btnDel').attr('disabled', true);
});







    </script>
</head>

<body>
    <div id="wrapper">
    <div class="sign-up_box">
        <form action="#" method="post" id="sign-up_area">
            <div id="entry1" class="clonedInput">
                <h2 id="reference" name="reference" class="heading-reference">Entry #1</h2>


               <fieldset>
                    <label class="label_ln" for="last_name">Last name:</label>
                    <input class="input_ln" type="text" name="last_name" id="last_name" value="">
                </fieldset>

            </div><!-- end #entry1 -->

            <div id="addDelButtons">
                <input type="button" id="btnAdd" value="add section"> <input type="button" id="btnDel" value="remove section above">
            </div>





            <fieldset class="form-actions">
                <input type="submit" value="Submit">
            </fieldset>
        </form>
    </div><!-- end sign-up_box -->
  </div>







      <div id="wrapper">
    <div class="sign-up_box">
        <form action="#" method="post" id="sign-up_area">
            <div id="entry1" class="clonedInput">
                <h2 id="reference" name="reference" class="heading-reference">Entry #1</h2>


               <fieldset>
                    <label class="label_ln" for="last_name">Last name:</label>
                    <input class="input_ln" type="text" name="last_name" id="last_name" value="">
                </fieldset>

            </div><!-- end #entry1 -->

            <div id="addDelButtons">
                <input type="button" id="btnAdd" value="add section"> <input type="button" id="btnDel" value="remove section above">
            </div>





            <fieldset class="form-actions">
                <input type="submit" value="Submit">
            </fieldset>
        </form>
    </div><!-- end sign-up_box -->
  </div>







    </body>
</html>
stark
  • 38
  • 5

2 Answers2

1

You cannot simply clone elements which have an id attribute. There mustn't be two elements with the same id on any single page. So please change the ids of the cloned elements accordingly, and the references to them in the code.

Ram Tobolski
  • 346
  • 1
  • 9
  • I think OP did consider that and actually assigned new IDs to the cloned elements — however, his two HTML forms share the same ID, so is `btnAdd`, `addDelButtons` and the likes. – Terry Jan 28 '15 at 23:58
1

Id tags must be unique within an HTML document or they won't work right. You'll probably want to "class" instead of "id" and use jQuery class selectors to select the appropriate fields, depending on what exactly you're trying to do.

Here are a few existing StackOverflow questions related to this:

Using same ID for in multiple HTML tags?

Several elements with the same ID responding to one CSS ID selector

javascript duplication ID conflict

Community
  • 1
  • 1
  • Thank you for your answer and i know your are right but the problem is i am not sure how to add class if you can show me how and where to put class it would be great – stark Jan 29 '15 at 00:19