0

I am looking for a functionality in my user interface:

  1. Add a new parent row (class="schedule") on focusout of the last column of the last parent row (class="schedule").
  2. And also add new child rows (class="from_hour_row", class="to_hour_row", class="mw_row") in the last 3 columns in the same parent row (class="schedule").
  3. Add new child rows (class="from_hour_row", class="to_hour_row", class="mw_row") in the last 3 columns in the same parent row (class="schedule") on focusout of the last child row (class="mw_row") of the last column without adding a new parent row if the current parent row is not the last one.

However, with my code I am only able to add new child rows and a new parent row on focusout of the last child row of the last column of the first parent row. Focusout of the last column of new parent row doesn't trigger any event. Always, it is the last child row of the last column of the first parent row that adds new child rows in the first row and a new parent row.

Attached a pic

form pic

Below is the html part

    <table class="bordered2" style="width: 900px; margin-top: 5px;" align="center">
  <thead>
    <tr>
      <th class="cntr">
        From Date
      </th>
      <th class="cntr">
        To Date
      </th>
      <th class="cntr">
        From Hours
      </th>
      <th class="cntr">
        To Hours
      </th>
      <th class="cntr">
        MW
      </th>
      <th class="cntr">
        MW-Hours
      </th>
    </tr>
  </thead>
  <tbody class="tbody">
    <tr class="schedule">
      <td>
        <input type="text" name="fromDate" class="fromDate" id="fromDate" />
      </td>
      <td>
        <input type="text" name="toDate" class="toDate" id="toDate" />
      </td>
      <td>
        <table class="from_hour" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0">
          <tr class="from_hour_row">
            <td>
              <input type="text" name="fromHour" class="fromHour" id="fromHour" size="10" />
            </td>
          </tr>
        </table>
      </td>
      <td>
        <table class="to_hour" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0">
          <tr class="to_hour_row">
            <td>
              <input type="text" name="toHour" class="toHour" id="toHour" size="10" />
            </td>
          </tr>
        </table>
      </td>
      <td>
        <table class="mw" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0">
          <tr class="mw_row">
            <td>
              <input type="text" name="mw" class="mw" id="mw" value="0.00" size="10" />
            </td>
          </tr>
        </table>
      </td>
      <td>
        <table class="mw_hr" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0">
          <tr class="mw_hr_row">
            <td>
              <input type="text" name="mwhrs" class="mwhrs" id="mwhrs" />
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr class="sumtr" id="sumtr">
      <td id="sumtd" colspan="5">
        Total Mw-Hr : 
      </td>
      <td name="totalmwhrs" class="totalmwhrs" id="totalmwhrs">
        0
      </td>
    </tr>
  </tbody>
</table>

Below is the jquery part

    $('table.mw').on('focusout', 'tr.mw_row:last td input',function(){
        $(this).parent().parent().parent().parent().parent().parent().find('table.from_hour tr.from_hour_row').last().after('<tr class="from_hour_row"><td><input type="text" name="fromHour" class="fromHour" id="fromHour" size="10"/></td></tr>');
        $(this).parent().parent().parent().parent().parent().parent().find('table.to_hour tr.to_hour_row').last().after('<tr class="to_hour_row"><td><input type="text" name="toHour" class="toHour" id="toHour" size="10"/></td></tr>');
        $(this).parent().parent().parent().parent().parent().parent().find('table.mw tr.mw_row').last().after('<tr class="mw_row"><td><input type="text" name="mw" class="mw" id="mw" value="0.00" size="10"/></td></tr>');
        $(this).parent().parent().parent().parent().parent().parent().find('table.mw_hr tr.mw_hr_row').last().after('<tr class="mw_hr_row"><td><input type="text" name="mwhrs" class="mwhrs" id="mwhrs" value="0.00" size="10"/></td></tr>');
    });

    $('table.mw:last').on('focusout', 'tr.mw_row:last td input',function(){
                    $(this).parent().parent().parent().parent().parent().parent().parent().find('tr.schedule').last().after('<tr class="schedule"><td><input type="text" name="fromDate" class="fromDate"/></td><td><input type="text" name="toDate" class="toDate"/></td><td><table class="from_hour" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0"><tr class="from_hour_row"><td><input type="text" name="fromHour" class="fromHour" size="10"/></td></tr></table></td><td><table class="to_hour" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0"><tr class="to_hour_row"><td><input type="text" name="toHour" class="toHour" size="10" /></td></tr></table></td><td><table class="mw" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0"><tr class="mw_row"><td><input type="text" name="mw" class="mw" value="0.00" size="10"/></td></tr></table></td><td><table class="mw_hr" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0"><tr class="mw_hr_row"><td><input type="text" name="mwhrs" class="mwhrs" /></td></tr></table></td></tr>');
    });
3
  • 1
    You might look into the parents() method as well, .parent().parent().parent().parent().parent().parent() needs some loving. Commented Oct 16, 2014 at 5:50
  • @helion3 As I debug, I see the newly added parent row and the child row are not being accessed through their class names because the events are not triggered on focusout of the newly added rows (new parent row and the new child row in it). Commented Oct 16, 2014 at 6:21
  • You can't listen for events on newly added DOM elements, that's why the accepted answer uses delegation. Commented Oct 16, 2014 at 15:38

1 Answer 1

1
  • First CSS selector doesn't have :last, only :last-child
  • Second, stop use parent() hell look to .closest().
  • Third, event not fire because your document doesn't know about events for new DOM elemets, you should use $(document).on("click", ".selector", function() {});

JS code for your purpose:

$(document).on('focusout', 'tr.mw_row:last-child td input',function(){
        $(this).closest("tr.schedule").find('table.from_hour tr.from_hour_row').last().after('<tr class="from_hour_row"><td><input type="text" name="fromHour" class="fromHour" id="fromHour" size="10"/></td></tr>');
        $(this).closest("tr.schedule").find('table.to_hour tr.to_hour_row').last().after('<tr class="to_hour_row"><td><input type="text" name="toHour" class="toHour" id="toHour" size="10"/></td></tr>');
        $(this).closest("tr.schedule").find('table.mw tr.mw_row').last().after('<tr class="mw_row"><td><input type="text" name="mw" class="mw" id="mw" value="0.00" size="10"/></td></tr>');
        $(this).closest("tr.schedule").find('table.mw_hr tr.mw_hr_row').last().after('<tr class="mw_hr_row"><td><input type="text" name="mwhrs" class="mwhrs" id="mwhrs" value="0.00" size="10"/></td></tr>');
    });

$(document).on('focusout', 'tr.mw_hr_row td:last-child input',function(){
                $(this).closest("tbody.tbody").append('<tr class="schedule"><td><input type="text" name="fromDate" class="fromDate"/></td><td><input type="text" name="toDate" class="toDate"/></td><td><table class="from_hour" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0"><tr class="from_hour_row"><td><input type="text" name="fromHour" class="fromHour" size="10"/></td></tr></table></td><td><table class="to_hour" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0"><tr class="to_hour_row"><td><input type="text" name="toHour" class="toHour" size="10" /></td></tr></table></td><td><table class="mw" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0"><tr class="mw_row"><td><input type="text" name="mw" class="mw" value="0.00" size="10"/></td></tr></table></td><td><table class="mw_hr" width="100%" cellspacing="0" cellpadding="0" bordercolor="#111111" border="0"><tr class="mw_hr_row"><td><input type="text" name="mwhrs" class="mwhrs" /></td></tr></table></td></tr>');
        });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanx a ton. Did a few minor changes to your solution to fit my requirement and it worked well.
to that i would like to add one more change, use deep clone .clone(true) and copy elements instead of writing html directly in js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.