I have a small program that includes a table with rows, each row contains an 'i' image. When you click the 'i' next to the element, it will open a more detailed view of the element, when you click the 'i' again of the SAME element, it should close the more detailed view (your generic toggle function). However, I need to have additional functionality that when you click the 'i' of a different element, it doesn't close the detailed view and just updates the right side. This part I was able to get working (first function). What I am having trouble with is then closing the detailed view if the same 'i' was clicked.
function toggleDetailsView() {
let parentId;
if (dom.$detailsView.is(':hidden')) {
showDetailsView();
parentId = $(this).parent('tr').get(0).id;
} else {
if (parentId) {
console.log(parentId);
if (parentId !== $(this).parent('tr').get(0).id) {
hideDetailsView();
}
}
}
}`
Therefore, I thought I could store the id of each row (each tr has a unique id) and then compare the two id's. If the detailed view is open and the id's are the same, close the detailed views. If the detailed view is open and the id's are different keep the detailed views open. If the detailed view is hidden just make it visible. I am trying to do that in the second function but I'm running into some issues. Namely, I'm just not sure how to store the prev and current id of the element to check against them.
function toggleDetailsView() {
const initialParentId = $(this).parent('tr').get(0).id;
let nextParentId;
if (dom.$detailsView.is(':hidden')) {
showDetailsView();
} else if (dom.$detailsView.is(':visible')) {
nextParentId = $(this).parent('tr').get(0).id;
if (nextParentId === initialParentId) {
hideDetailsView();
} else if (nextParentId !== initialParentId) {
showDetailsView();
}
}
}`
Thanks to anyone who is able to help!