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 trialTetsuya Yokoyama
11,286 PointsWhy "preventDefault()" not working???
In this REVP code, there's "e.preventDefault()" in submit function. But when I hit submit without input value. It creates list element as usual. Shouldn't it be stopped because there's no value(=default)??
Thanks in advance!!
2 Answers
Steven Parker
231,248 PointsThe "default" that is prevented is the default behavior of a submit event, which would be to post the form data back to the server. So that seems to be working.
The behavior of creating a list element is all due to the code right here in the handler, which doesn't test the value before proceeding (but you could add a test).
Tetsuya Yokoyama
11,286 PointsWow, Thank you very much!!
Tetsuya Yokoyama
11,286 PointsHi Steven, I appreciate that you taking time to reply this. Sorry my bad, this is the code snapshot There's "e.preventDefault()" on line 60.
form.addEventListener('submit', (e) => {
e.preventDefault();
const text = input.value;
input.value = '';
const li = createLI(text);
ul.appendChild(li);
});
Thanks!!
Dylan Bailey
Front End Web Development Techdegree Graduate 17,232 PointsThat method won't stop you from submitting without an input value. It stops the default action. And the default action of submit, on forms, is to refresh the webpage. So this particular method is stopping that from happening, so we can continue to populate our <ul> with <li> elements :)
Steven Parker
231,248 PointsSteven Parker
231,248 PointsPlease provide a specific time index in the video where this can be seen. I only saw edit/save function code and didn't find where an item was being added.