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 trialGabriel C. Cavallo
4,438 PointsJQuery and JS together?
I thought that JQuery and plain JS could not coexist in the same js file. I'm glad I was wrong... so a part of a js file can be written in plain JS and another part in JQuery, are there any limitations, constraints?
2 Answers
Liam Clarke
19,938 PointsHi Gabriel
There are no constraints to having Native JS and Jquery in the same file.
Jquery is just a JavaScript library, think of it as a collection of functions (plugins) that you can use to write less code but behind the scenes, the same native JavaScript is getting used.
Heres an example:
DOM selectors
In JQuery
var someClass = $(".someClass");
Behind the scenes, the same native JS code is getting used like
var someClass = document.querySelectorAll(".someClass");
Because of this, we can use both native and jquery at the same time.
William Mead
9,937 PointsI agree that it seems weird to introduce jQuery into this project. Certainly, there is nothing wrong with it, but since the project already includes quite a bit of CSS, why not just animate the falling of the piece with CSS? That way, in JS, all we would need to do is add a class to the selected token.
Joseph Cowan
6,231 PointsWilliam Mead Well I think writing a lot of the animations in CSS would actually 'cost' more lines of code instead of just using simple jq methods and animations. Rewriting some jQ from vanilla JS and CSS can be extremely expensive in developer time and in code space (which I'm a fan of reducing in general), and it also creates more areas for errors to occur. It's actually not uncommon to see this mixture of JavaScript and jQuery, especially when it comes to animation because a lot of jq is essentially 'better-packaged' CSS and JS. Just my two cents.
Zimri Leijen
11,835 PointsZimri Leijen
11,835 PointsjQuery is just a library of JS.
JS is exactly so popular because of all the libraries it has.
A library is basically a set of scripts that other developers have written and shared, because they're useful and reusable.
because of this, all of the JS libraries can be used together with JS in the same file, because they are in fact JS.