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 trialRobert Bojor
Courses Plus Student 29,439 PointsEasier Javascript solution
Not really a question but another shorter / easier JS solution for the delete/destroy.
First add another class to the delete button, let's say "delete_item".
Next, add the following code into your js file
$(function(){})
.on('click', '.delete_item', function(e){
if (!confirm('Are you sure?')) {
e.preventDefault();
}
})
The script will break the submit flow of the form, ask you if you are sure and if Yes is clicked it will resume the submit. If cancelled, the initial event is prevented and nothing happens.
The script is a bit easier to understand and not so heavy on the typing. Plus it works even if you change the form to a link or any other element provided you keep that "delete_item" class assigned to it.
Greg Kaleka
39,021 PointsHey Robert,
Per Bryan Knight's suggestion, I changed the class you use in the function to "delete_item" (and added code highlighting :).
Just want to make sure future readers (like me!) don't get tripped up.
2 Answers
Robert Mylne
13,708 PointsEven easier is to add this to the form
<form onsubmit="return confirm('Are you sure you want to delete this?')">
...
</form>
Laurie Williams
10,174 PointsYou could also give your button a class of .deletebtn and then call the jquery function like this:
$( document ).ready(function() {
$('.deletebtn').click(function(e)
{
if(confirm("Are you sure?"))
{
//deletes the list item
}
else
{
e.preventDefault();
}
});
});
Bryan Knight
34,215 PointsBryan Knight
34,215 PointsThis is an awesome refactor, just one issue. You have "delete_item" for the class but "delete_list" for the on click function. Had to change one of these to match to get it to work. Otherwise great suggestion!!