i need some help,
i need that only the selected template is displayed on the screen, and all others are removed, so the expected result should be that those values:
<div class="city">City<span>,</span></div> <div class="country">Co</div>
would be replaced from the selected template values.
In short, what I need to do is for the user to choose a city from all those shown and when they click on the desired city, js obtains the values and shows them on the screen, removing all the other options.
const userLocationContainer = document.querySelector("[data-location-container]")
const locationTemplate = document.getElementById("location-template");
function setValue(selector, value, { parent = document } = {}) {
parent.querySelector(`[data-${selector}]`).textContent = value
}
userLocationContainer.addEventListener('click', handleLocation);
function handleLocation(location) {
const clonedTemplate = locationTemplate.content.cloneNode(true);
setValue("city", location.city, { parent: clonedTemplate });
setValue("country", location.country, { parent: clonedTemplate });
// Clear previous content of userLocation and append the cloned template
userLocationContainer.innerHTML = '';
selectedLocation.append(...clonedTemplate.childNodes);
}
</div>
<div class="locations" data-location-container>
<div class="mainPlace" id="userLocation">
<div class="city">City<span>,</span></div>
<div class="country">Co</div>
</div>
</div>
</div>
</header>
<template id="location-template" data-location>
<div class="mainPlace-template">
<div class="city" data-city></div><span>,</span>
<div class="country" data-country></div>
</div>
</template>
Any help will be greatly appreciated.
2
Answers
There is a canonical way of doing this, which is to use a common class, and on click, hide all elements of that class and then show the selected one.
From what I understand, you need kind of this feature (assuming that you need dropdown as mentioned users will choose)