Most of my work lately has been in WordPress and jQuery is built in.
$("#id") is easier to type than document.getElementById("id").
While I'm still learning more and more about React, I plan on eventually figuring out the setup for a React Frontend with a WordPress backend. Frameworks like Frontity make that connection a whole lot easier.
I think your first reason is the main cause of that eighty-something percentage of websites. There is a huge number of websites based on wordpress, and they all have jQuery.
I personally write a mix of vanilla JS and jQuery when writing javascript in wordpress. In most cases I prefer vanilla JS, but sometimes jQuery just has features ready that I need and... it's just there, you know?
It's not always safe to use this trick to query an element. For example, if you have <div id="alert" /> on a page, accessing window['alert'] will not return the element you're looking for.
Thats a great tip Alan! I agree that it is overkill and I haven't used it for a number of years. I think when I stopped using it I became a better developer and got a much better understanding of js.
I use it. 🙋♂️
$("#id")
is easier to type thandocument.getElementById("id")
.While I'm still learning more and more about React, I plan on eventually figuring out the setup for a React Frontend with a WordPress backend. Frameworks like Frontity make that connection a whole lot easier.
I think your first reason is the main cause of that eighty-something percentage of websites. There is a huge number of websites based on wordpress, and they all have jQuery.
I personally write a mix of vanilla JS and jQuery when writing javascript in wordpress. In most cases I prefer vanilla JS, but sometimes jQuery just has features ready that I need and... it's just there, you know?
Fun fact
If you need to access element via ID, then you can skip functions altogether.
Having element:
<div id="my-element"></div>
you can access it viawindow["my-element"]
;-)That is a fun little fact.
Good to know!
It's not always safe to use this trick to query an element. For example, if you have
<div id="alert" />
on a page, accessingwindow['alert']
will not return the element you're looking for.If you really just love
$()
you can add this line to your JSor this one if you don't want to compile to es5
And remove JQuery as it's overkill to have your user download a whole library if that's all you want XD
Thats a great tip Alan! I agree that it is overkill and I haven't used it for a number of years. I think when I stopped using it I became a better developer and got a much better understanding of js.