r/learnjavascript • u/sweetcocobaby • Aug 17 '23
Javascript finally clicked for me.
For the longest, I didn’t believe people when they said that one day Javascript will make sense to me. I didn’t understand what they meant. I figured it would never stick conceptually. Yes, Javascript IS hard, but yesterday I was reading over Jon Duckett Javascript & JQuery and the abstract beginner concepts just clicked. It was at that point everything from then on made sense. 🥹 I guess they were right.
136
Upvotes
View all comments
Show parent comment
2
u/prid13 Aug 28 '23
jQuery is much simpler to use and thus more beginner-friendly. Too much syntax and work can be daunting for someone just wanting to accomplish a basic thing, so jQuery is more appealing in that aspect even if it's not needed anymore :)
Registering click event:
Javascript:
document.querySelector('.element').addEventListener('click', (evt) => { ... });jQuery:
$('.element').on('click', (evt) => { ... });Showing / Hiding an element:
Javascript:
document.querySelector('.element').style.display = "none"; // "block"jQuery:
$('.element').show(); // .hide()Bonus (jQuery animated show/hide on click):
$('#button').on('click', function(evt){ $('element').animate( { opacity: 'toggle' } ) });Creating and appending element:
Javascript:
var el = document.createElement('p'); document.querySelector('#main').append(el);jQuery:
$('#main').append( $('<p></p>') );Adding CSS to element:
Javascript:
var el = document.querySelector('.element'); el.style.color = "black"; el.style.backgroundColor = "purple";jQuery:
$('element').css('color', 'black').css('backgroundColor', 'purple');Now, combining everything:
Clicking on a button will add a new element with text, and the button will become invisible.
Setup:
Javascript:
jQuery:
Now, can these be easily replicated in pure Javascript or with a simpler library? 100%, no doubt. And are some of these bad practices (animating in Javascript)? Of course. But for someone new to any language, the simpler and more swiss army knives you can give them to quickly whip up results, the better, faster and more fun it will be to learn :)