I’ve tried this code that was posted but I can’t get it to work the way I need. I can get it to create both fields but I can’t make them look the same as the format of the first fields. How can I Add the entire section and make it look the same? and control the maximum number of activities inserted.
TIA
This is what I’m trying to acomplish
const addActivity = document.getElementById("add");
var i = 0;
const activityDiv = document.getElementById("Activity");
addActivity.addEventListener("click", function() {
i++;
const newspan = document.createElement('div');
newspan.className = "activityGroup";
const removeButton = document.createElement('button');
removeButton.addEventListener('click', function(e) {
e.target.closest(".activityGroup").remove();
});
removeButton.className = "delbtn";
removeButton.innerHTML = "X";
//
const txtfield = document.createElement('input');
const txtarea = document.createElement('textarea');
//
txtfield.id = 'activity_' + i;
txtfield.placeholder = "Activity " + i;
newspan.appendChild(txtfield);
//
newspan.appendChild(txtarea);
txtarea.id = 'activity_description_' + i;
txtarea.placeholder = "Activity Description " + i;
newspan.appendChild(removeButton);
activityDiv.appendChild(newspan);
});
.delbtn{color:red;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="Activity">
<div class="activityGroup">
<input placeHolder="Type your activity" />
<br>
<br>
<textarea rows="5" cols="50" placeHolder="Type your descvription"></textarea>
<button class="delbtn">X</button>
</div>
<!-- Remove button -->
</div>
<button id="add">Add Activity</button>
2
Answers
Do you mean "If I have Activity 1, 2, 3, 4, 5 and Delete 3 I want to be able to get Activities 1, 2, 4, 5?"
From what I’ve gathered, you want a button that adds and deletes activities but you only want it to work when previous activity isn’t empty.
In the javascript, when you handle the buttons click event check previous activity, if it’s empty, return, if it isn’t empty, execute the add/delete code.
First, the problems:
This is likely because in your original
.activityGroup
there are a number of<br>
elements between the<input>
and the<textarea>
elements – incorrectly – used for spacing which you haven’t inserted into the copy.Also, two of the attributes of the original
<textarea>
(rows="5"
, andcols="50"
) are omitted from the one you create within the function, not to mention the line-breaks and other white-space.This can be rectified by simply creating, and appending, those other elements:
As to your question:
Controlling the number of total activities/sections on the page is relatively easy and simply requires you to check the number of existing sections before adding a new one:
Now, I’d like to improve the above functions as follows with explanatory comments in the code itself:
JS Fiddle demo.
References:
align-self
.box-sizing
.content
.display
.gap
.grid-column
.grid-template-columns
.grid-template-rows
.inline-size
.margin
.margin-block
.margin-inline
.min-block-size
.padding
.padding-block
.padding-inline
.repeat()
.var()
.document.createElement()
.document.querySelector()
.document.querySelectorAll()
.Element.append()
.Element.querySelector()
.Element.querySelectorAll()
.Element.remove()
.Event.target
.Event.currentTarget
.EventTarget.addEventListener()
.HTMLElement.dataset
.Node.appendChild()
.Object.assign()
.yield
.