PaulOB
March 7, 2021, 1:53pm
25
[offtopic]
I know its not the point but if you styled the items in css using :nth-child then you can let css do it automatically.
e.g.
li:nth-child(1) input,
.color1 {
background-color: #4000ff;
}
li:nth-child(2) input,
.color2 {
background-color: #8000ff;
}
li:nth-child(3) input,
.color3 {
background-color: #bf00ff;
}
li:nth-child(4) input,
.color4 {
background-color: #ff00ff;
}
li:nth-child(5) input,
.color5 {
background-color: #ff00bf;
}
li:nth-child(6) input,
.color6 {
background-color: #ff0080;
}
Assuming you have removed the classed from the inputs of course. I know styling wasnât the point of the exercise but just wanted to say
[/offtopic]
3 Likes
Great, so on any delete operation, our task is to program so that the last child of ol
is deleted.
Why do you come to that logic?
If I push the delete button on Row 3⌠I want to delete Row 3, not Row 5. I want the data I have input into row 4 to now be on row 3.
2 Likes
1,2,3,4,5,6
1,2,3,4,5
1,2,3,4
1,2,3
1,2
1
classes are arranged in this order on every delete operation, subsequently.
The classes are, yes.
But thereâs an <input>
in that row.
Type âHere I amâ in the last input, and click the delete button in Row 3.
Whereâd your data go?
1 Like
m_hutley:
The classes are, yes.
PaulOB:
li:nth-child(1) input,
.color1 {
background-color: #4000ff;
}
li:nth-child(2) input,
.color2 {
background-color: #8000ff;
}
li:nth-child(3) input,
.color3 {
background-color: #bf00ff;
}
li:nth-child(4) input,
.color4 {
background-color: #ff00ff;
}
li:nth-child(5) input,
.color5 {
background-color: #ff00bf;
}
li:nth-child(6) input,
.color6 {
background-color: #ff0080;
}
As long as we are deleting from the bottom. Isnât everything in place? Classes + text?
I got it OP doesnât want to change the text when deleted and bottom item pushed up.
1 Like
The OPâs head isnât with it at the moment. I think a break from the screen and a stiff drink is called for.
3 Likes
Hi there,
I merged @PaulOB css + small JS:
Its working:
document.querySelector('#parent').addEventListener('click', function(e) {
var removeTarget = e.target.parentNode;
removeTarget.parentNode.removeChild(removeTarget);
}, false);
1 Like
var removeTarget = e.target.parentNode = this will give the parent of the element where the click has occurred in this case li
.
removeTarget.parentNode =
ol`
emoveTarget.parentNode.removeChild = These are all `li
If we change this â
removeTarget.parentNode.removeChild(removeTarget);
to
removeTarget.parentNode.removeChild();
All children(li
) should delete, but that is not happening.
codeispoetry:
If we change this â
removeTarget.parentNode.removeChild(removeTarget);
to
removeTarget.parentNode.removeChild();
All children( li
) should delete, but that is not happening.
Thatâs asking the parent node to remove a child, without telling it what child to remove. That is not going to work.
A simpler way is to use the remove method instead of removeChild.
var removeTarget = e.target.parentNode;
removeTarget.remove();
That works, but the removeTarget name is somewhat confusing.
We can help to alleviate some of that confusion by making it more explicit what e.target
is.
var deleteButton = e.target;
deleteButton.parentNode.remove();
That way, we now know that itâs the button parent that is being removed, which in this case is the <li>
element.
2 Likes
Thanks.
Agree!
removeTarget was slightly misleading.
system
Closed
June 7, 2021, 1:44pm
39
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.