tags:

views:

162

answers:

3

I have a form which can have up to 6 children added to it. Therefore there will be 6 sets of the following HTML:

            <table class="portletTable child" cellpadding="0" cellspacing="0" border="0" summary="Please enter some more details regarding your dependants">
            <tr>
                <th>
                    <label for="indTitle">Title</label>
                </th>
                <td colspan="3">
                    <select id="indTitle" class="inlineSpace">
                        <option value="Please select">Please select...</option>
                        <option value="Mr">Mr</option>
                        <option value="Mrs">Mrs</option>
                        <option value="Ms">Ms</option>
                        <option value="Miss">Miss</option>
                        <option value="Dr">Dr</option>
                        <option value="Other">Other</option>
                    </select>
                    <label for="indOther" class="inlineSpace">Other</label>
                    <input type="text" class="text" name="indOther" id="indOther" maxlength="20" />
                </td>
            </tr>
            <tr>
                <th>
                    <label for="firstname">First name</label>
                </th>
                <td colspan="3">
                    <input type="text" class="text" maxlength="50" value="" id="firstname" />
                </td>
            </tr>
            <tr>
                <th>
                    <label for="lastname">Last name</label>
                </th>
                <td colspan="3">
                    <input type="text" class="text" maxlength="50" value="" id="lastname" />
                </td>
            </tr>       
            <tr>
                <th>
                    <label for="dobDay">Date of birth</label>
                </th>
                <td colspan="3">
                    <select name="dobDay" id="dobDay" class="inlineSpace">
                        <option value="day">Day</option>
                    </select>
                    <label for="dobMonth" class="offScreen">Month</label>
                    <select name="dobMonth" id="dobMonth" class="inlineSpace">
                        <option value="month">Month</option>
                    </select>
                    <label for="dobYear" class="offScreen">Month</label>                
                    <select name="dobYear" id="dobYear">
                        <option value="year">Year</option>
                    </select>
                    <p class="fieldNote">You must be over 'minimum partner age' and under 'max partner age'</p>             
                </td>
            </tr>
            <tr>
                <th>
                    Gender
                </th>
                <td colspan="3">
                    <input id="male" name="childGender" class="radio" type="radio" />
                    <label for="male" class="inlineSpace">Male</label>
                    <input id="female" name="childGender" class="radio" type="radio" />
                    <label for="female">Female</label>
                </td>
            </tr>               
        </table>

I need the first child to show by default and the following five to be hidden from view.

When a user clicks the following link i want the second child to show, if they click it again then the third child inputs show and so on...

<tr>
                <th class="dependant">
                    <a href="" class="add">Add another child to your policy</a>
                </th>
            </tr>

Obviously when the sixth child is shown the link should not display.

I also need the reverse to be true in that a user has the option to remove the latest added child with this hyperlink:

<tr>
                <th class="dependant">
                    <a href="" class="remove">Remove this child from your policy</a>
                </th>
            </tr>

If Javascript is turned off then all will show by default.

Thanks in advance if any of you can help.

A: 

You can put all tables with separated IDs, and manipulate their visibility state with document.getElementById("table_id").style.display = "none" or document.getElementById("table_id").style.display = "block".

Alexandru Mos
+1  A: 

This should do it:

$(function() {
    // Hide everything except the first one
    $('.portletTable').not(':first').hide();

    // Remove functionality
    $('.dependant .remove').click(function() {
        // Hide this child and move it to the end of the table
        $(this)
            .closest('.portletTable')
            .hide()
            .appendTo($(this).closest('form'));
    });

    // Show functionality
    $('.dependant .remove').click(function() {
        // Show the first hidden table
        $(this).closest('form').find('.portletTable:hidden:first').show();
    });
});

That should give you the functionality you want. You probably want to improve the remove functionality by resetting all inputs and selects to their default state.

Tatu Ulmanen
+1  A: 
$('.add').live('click',
   function() {
       //seek next element with class "child"
       var $nextChild = $(this).parents('.child').next('.child :first');  
       if ($nextChild.size() > 0)  //exists?
           $nextChild.show();  //show it
   }
);

$('.remove').live('click',
   function() {
       //hide the parent with class "child"
       $(this).parents('.child').hide();
   }
);

Replace live with bind if you don't plan to add links later.

Alex Bagnolini