Design comparison
Solution retrospective
Hello, i need help with this project! Right now whenever a user submits a valid link they get a short link in a container below, those links go down the page. How to limit them? For example limit to 3, so submitting a new link will remove the last link in the container and so on, to make it seem like a loop, to make old links disappear. So I want to solve this endless container going down the page. Does anyone have any tips, or thoughts on how it can be done? Thank you!
Community feedback
- @ajay117Posted about 2 years ago
Hi Abrosss, I saw your project and I can say you did great. The easiest solution you can do is to write a condition, like whenever the "ul" with id of "results" have more than 3 child nodes (i.e. 'li') you can put below code in condition body.
//Create a nodeList with all child "li" in the variable... let allList = document.querySelectorAll('#results li'); //Convert nodeList to Array... let allListArr = Array.from(allList); //Selects the "results" container let parent = document.querySelector('#results'); //Remove the first list from the parent container parent.removeChild(allListArr[0]);
Hope it helps. Good Luck!
Marked as helpful1@AbrosssPosted about 2 years ago@ajay117 omg! thank you so much! Maybe i did something wrong but the array method didn't really work but i understand the idea! Thanks to you i somehow did what i wanted to do and what i needed help with:D so the limit now is 3 elements in the list and when a user wants to add another, the third element gets removed and so on. Let me know what you think:) Though the next section after the list jumps annoyingly now. Still haven't figured out how to fix it. Thank you so much for your help!
1@ajay117Posted about 2 years ago@Abrosss glad to help, but now when you enter the same URL the list acts weird. The code which I talked about is:-
function updateAndRemoveCurrentUrlAndList() { let allList = document.querySelectorAll("#results li"); let allListArr = Array.from(allList); let parent = document.querySelector("#results"); if (allListArr.length > 3) { parent.removeChild(allListArr[0]); currentUrl.splice(0, 1); } }
You just have to put this function inside getLinksData(url) function after displayShortLink(res.result) at 419b95c in git history before your latest commit. I created a pull request so you can see the code, please do not merge it though, because it will affect your CSS.
Good luck! Happy Coding
Marked as helpful1@AbrosssPosted about 2 years ago@ajay117 thank you so much for your time! It works:D i only changed parent.removeChild(allListArr[0]) to parent.removeChild(allListArr[3]) so it deletes the last child, because i add items using prepend() and it inserts an element as the first child. THANK YOU for helping me understand the solution and make it work! Also when i add the same url it works ok now. Good luck to you too! :)
1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord