Yusril Maulidan Raji Yusril Maulidan Raji - 4 months ago 8
CSS Question

How to give specific attribute to the last process of insertAfter() in jQuery?

Is there anyone who knows on how to give particular attribut (a class in this case), on the last attempt of insertAfter() in jQuery?

For instance,

Before:

<td class="column-timesheet-travel">
</td>
<td class="column-timesheet-travel">
</td>


After
$('<td class="column-timesheet-work"></td>').insertAfter(".column-timesheet-travel")
, it should be something like:

<td class="column-timesheet-travel">
</td>
<td class="column-timesheet-work">
</td>
<td class="column-timesheet-travel">
</td>
<td class="column-timesheet-work NewClassAddedOnLastAttempt">
</td>


Or is there any other better way to do this?
Thank you in advance.

Answer

Before insert the HTML update store the inserted div on a var and them insert anywehre you need:

var $div = $('<td class="column-timesheet-work YOU-COULD-PUT_YOUR-CSS-HERE">TD 3</td>').addClass('or-add-this-way'); $('.column-timesheet-travel').last().after($div);

The example could be done in two ways, adding the class you need, replacing " YOU-COULD-PUT_YOUR-CSS-HERE" or using the method addClass of jquery.

https://jsfiddle.net/wallynm/2rjyLd7m/

Comments