Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Rohit Sagar
5,938 PointsPls, help me achieve this .....
https://codepen.io/Cheeko/pen/GXOPvR
summary of my Project:------------ You can ad add and remove words or item you type in the input field by clicking add button and remove added item by clicking remove button.
But I want something else to happen......
When someone tries to click remove item button if there is no li element then i want to show an alert message that there is nothing to remove ...
(Pardon my eng)
pls, let me know if my code is not well structured or written... I m learning javascript.
there is my code on Codepen
2 Answers
Sam Gord
14,084 Pointsall u need is a simple if statement in your remove button event listener,
// removeButton event
removeButton.addEventListener('click', function() {
if(document.querySelector('li:last-child')){
li = document.querySelector('li:last-child');
rainbow.removeChild(li);
}else{
alert('there is nothing to remove');
}
});
by writing the condition like this -->
if(document.querySelector('li:last-child'))
i'm simply saying that if there was an li item go on and remove it , but if there wasn't any li item then the result of this if will be false and then the else block fires which is the alert message that we want ;)
happy coding
Rohit Sagar
5,938 PointsThanks sam .. I was having trouble achieving that ...
Thank YOU
Sam Gord
14,084 Pointsyou are very welcome , all the bests