Other Pages

jQuery vs Javascript

Goals

  • Write some straight JavaScript

  • Compare that JavaScript to jQuery

Steps

Step 1

On the last page, we wrote some jQuery that looked like this. Let's comment it out for now, and see if we can replicate the same functionality in straight JavaScript. Add comment tags before these lines. In JavaScript, the easiest way to comment out a line is to put a // at the beginning. Go ahead and try this:

//$("#copyright").click(function(){ 
//  $(this).css('color','purple'); 
//});"

Now if you save the page, refresh, and click the copyright line, nothing should happen.

Step 2

Underneath the lines you just commented out, let's take a shot at writing the same functionality in straight JavaScript. There are a number of ways to do this. Here's an example:

document.getElementById('copyright').addEventListener("click", function() {
  this.style.color = 'purple';
});

If you save the page, refresh, and click, you should see the same visual result (purple text). jQuery's click is very similar to using addEventListener with "click". Read more about addEventListener on MDN.

Explanation

So Should I Use regular JavaScript or jQuery?

Don't forget that everything you can do with jQuery you can do with regular JavaScript. If you want to start building cool user interfaces that use JavaScript right away, jQuery will probably help you do that the fastest. It's worth learning how to do things with regular JavaScript too, because you won't always want or need to use jQuery.

Next Step: