So I already have these elements in my page:
<button id="addMore" class="button" style="background-color: green;">+</button>
<button id="removeMore" class="button" style="background-color: red;">-</button>
<div id="fieldList">
<select style="width:100%" id="prisplan" name="prisplan[]" required>
<option selected value="">Velg prisplan</option>
<?php foreach($eachlines as $lines){ //add php code here
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
<input type="text" name="gsm[]" placeholder="GSM" required onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
</div>
When pressing the + button, it adds new elements. This works perfectly:
$(function() {
$("#addMore").click(function(e) {
e.preventDefault();
// Get the element of prisplan
var itm = document.getElementById("prisplan");
// Copy the element and its child nodes
var cln = itm.cloneNode(true);
// Append the cloned element to list
document.getElementById("fieldList").appendChild(cln);
$("#fieldList").append("<input type='text' placeholder='GSM' name='gsm[]' required onkeypress='return event.charCode >= 48 && event.charCode <= 57'>");
});
});
Now I also want a remove button. That button should remove the last added elements from "addMore" button, but never remove the original elements. How can I do this? The code below is what I've got so far, but that removes everything.
$(function() {
$("#removeMore").click(function(e) {
e.preventDefault();
$('#fieldList').remove();
});
});
How can I accomplish this?