I'm struggling with selecting all spans in the card using code that currently only selects the first span when I use getElementsByTagName
. Can anyone offer assistance as I have multiple cards containing spans?
TypeError: Failed to execute 'insertBefore' on 'Node': parameter 2 is not of type 'Node'. at HTMLDivElement.
Here is the JavaScript code snippet I am working on:
const last = document.getElementById('last');
last.addEventListener("click", (event)=>
{
if(event.target.tagName === 'I')
{
const icon = event.target;
const card = icon.parentNode;
if(icon.getAttribute('value') === 'remove')
{
last.removeChild(card);
}
else if(icon.getAttribute('value') === 'edit')
{
const span = card.firstElementChild;
const input = document.createElement('input');
input.type = 'text';
input.value = span.textContent;
card.insertBefore(input, span);
card.removeChild(span);
icon.className = 'fa fa-check-circle';
icon.setAttribute('id','green-color');
icon.setAttribute('value','');
}
else if(icon.getAttribute('value') === '')
{
const input = card.firstElementChild;
const span = document.createElement('span');
span.textContent = input.value;
card.insertBefore(span, input);
card.removeChild(input);
icon.className = 'fa fa-edit';
icon.setAttribute('id','');
icon.setAttribute('class','fa fa-edit edit');
icon.setAttribute('value','edit');
}
}
})
<div class="row last" id="last">
<h4 class="exam-header col-12">Schedule of exams dates</h4>
<a class="exam-info teacher-link text-md-center col-8" id="teacher-link" href="#">
<span class="subject subject-name">Computer Science</span>
<span class="subject subject-date">2021/12/2</span>
<span class="subject subject-time">9:00 AM</span>
<span class="subject subject-duration">2h</span>
<i class="fa fa-edit edit" value="edit"></i>
<i class="fa fa-times remove" value="remove"></i>
</a>
<a class="exam-info teacher-link text-md-center col-8" id="teacher-link" href="#">
<span class="subject subject-name">Computer Science</span>
<span class="subject subject-date">2021/12/2</span>
<span class="subject subject-time">9:00 AM</span>
<span class="subject subject-duration">2h</span>
<i class="fa fa-edit edit" value="edit"></i>
<i class="fa fa-times remove" value="remove"></i>
</a>
</div>