Reputation: 99
I would like to add some functionality with the below code that only 5 times one can click the add button.
Also I would like to add a delete button with every replicated so when clicked, div is deleted and the counter of the 5 times is decreased.
HTML:
<button id="button" onlick="duplicate()">Add another plan</button>
<div id="duplicater">
<p>Choose Your Mobile Plan</p>
<select>
<option value="1">Package 1</option>
<option value="2">Package 2</option>
<option value="3">Package 3</option>
<option value="4">Package 4</option>
<option value="5">Package 5</option>
<option value="6">Package 6</option>
</select>
</div>
JS:
document.getElementById('button').onclick = duplicate;
var i = 0; var original = document.getElementById('duplicater');
function duplicate() {
var clone = original.cloneNode(true); // "deep" clone
clone.id = "duplicater" + ++i; // there can only be one element with an ID
original.parentNode.appendChild(clone); }
A JSFiddle can be found here: http://jsfiddle.net/7x4re/
Upvotes: 1
Views: 2599
Reputation: 19093
You can add a button to delete the element like this:
<button id="button">Add another plan</button>
<div id="duplicater">
<p>Choose Your Mobile Plan</p>
<select>
<option value="1">Package 1</option>
<option value="2">Package 2</option>
<option value="3">Package 3</option>
<option value="4">Package 4</option>
<option value="5">Package 5</option>
<option value="6">Package 6</option>
</select>
<button class='removebutton'>Delete</button>
</div>
You can enable a function to fire when it's clicked using jquery:
$('body').on('click', ".removebutton", remove);
Finally, the remove handler can remove the clicked element:
function remove() {
if (count > 1) {
if ($(this).parent().attr('id') != 'duplicater') {
$(this).parent().remove();
count--;
} else {
alert("You can't delete the first plan");
}
} else {
alert("You can't delete the first plan");
}
}
I've used a similar approach to the counter as used by ReeCube.
As this uses jquery, you need to include it, either from your local machine or from somewhere else e.g.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
All the js code should be wrapped in the standard jquery ready function:
$(function(){
// Put your js code here.
});
Upvotes: 2
Reputation: 2597
You just have to add an if before your duplicate code:
document.getElementById('button').onclick = duplicate;
var original = document.getElementById('duplicater');
var i = 1;
function duplicate() {
if (i < 6) {
var clone = original.cloneNode(true); // "deep" clone
clone.id = "duplicater" + i++; // there can only be one element with an ID
original.parentNode.appendChild(clone);
}
}
If you want to disable the button when 5 times clicked, below is the code:
document.getElementById('button').onclick = duplicate;
var original = document.getElementById('duplicater');
var i = 1;
var max = 5;
function duplicate() {
if (i < max + 1) {
var clone = original.cloneNode(true); // "deep" clone
clone.id = "duplicater" + i++; // there can only be one element with an ID
original.parentNode.appendChild(clone);
if (i > max) document.getElementById('button').disabled = true;
}
}
2. And an example on JSFiddle.
Upvotes: 3