Here is the HTML setup I'm working with:
<div class='form-row'>
<div class='col-2'>
Blah Blah
</div>
<div class='col-4'>
<button type='button' class='add_row'>+</button>
</div>
</div>
When the add_row
button is clicked, I want to add another form-row
div right next to the closest existing form-row
div. The end result should look like this:
<div class='form-row'>
<div class='col-2'>
Blah Blah
</div>
<div class='col-4'>
<button type='button' class='add_row'>+</button>
</div>
</div>
<div class='form-row'>
<div class='col-2'>
Blah Blah
</div>
<div class='col-4'>
<button type='button' class='add_row'>+</button>
</div>
</div>
I attempted to use jQuery with the following script:
$(document).on('click', '.add_row', function () {
$(this).closest('.form-row').append("<div class='form-row'>div contents</div>");
});
However, it ends up adding the new div AFTER the direct parent of the button (<div class='col-4'>
).
What would be the correct jquery code to append the new div to the closest div with a specified class instead?