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

JavaScript

Does event.preventDefault() require jQuery?

At about 3:29 in the video, Dave states that the event.preventDefault() method is a "jQuery thing". But in the MDN documentation, there is no mention of jQuery, which may suggest that this method does not necessitate the use of jQuery, and, parenthetically, that this method does not originate from jQuery.

Is anybody certain on the origins of this method?

3 Answers

The good news is it's not specifically tied to jquery. preventDefault is a dom method per the w3. Read it here: https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation

I read the documentation, could you elaborate on that? I would like to understand myself. Does this mean we can use

function whatever(event) {
   event.preventDefault
}

without having the jQuery library CDN or download in our html file?

Yeah, check this out.

Disclaimer, I'm pretty noobish with pure js, I'll be the first to say that.

jQuery's example at the bottom of the page: https://api.jquery.com/event.preventdefault/

Rewritten without jQuery, go ahead, check under the JS settings, no jQuery needed: http://codepen.io/kevink/pen/zBWjpd

Thank you for taking the time to do that. Even watching and rewatching the same videos over and over there are definitely still many things I am missing.

Thanks, Kevin. I had a strong feeling that was the case but, at my experience level, I'm not in any position to refute the words of an instructor.

yes, it is apart of the jQuery library.

https://api.jquery.com/event.preventdefault/

Yup, it's a method that's included in the DOM API. The DOM version is actually a bit different than jQuery's though. While on the surface they do the same thing, internally the code is being handled a little differently. But yeah, you can use it without including jQuery.