About
In this code snippet, we’ll learn about jQuery.
jQuery is a library that makes working with javascript a bit easier and quicker. It’s not as popular or needed these days because of all the js frameworks. But it’s still present on a lot of older code. Also, I think it’s useful if you are making a smaller project and don’t need the features and complexity of a js framework.
Note: There are lot more jQuery functions than demonstrated here. This is the complete documentation. Also, I demonstrate some of those functions in other posts.
Adding jQuery
You can add it by using the <script> tag. The src can then be pointed to either a CDN(like in the example below) or to a jQuery file added to your project locally. jQuery download here.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Code:
let element; //Get element by id. element = jQuery("#someid"); //Get element by class. element = jQuery(".someclass"); //Get element by tag. element = jQuery("p"); //Remove element. jQuery("#someid").remove(); //Get perent element. element = jQuery("#someid").parent; //Show element. jQuery("#someid").show(); //If a number in miliseconds is put in as a parameter to show()/hide() //Hide element. jQuery("#someid").hide(); //Setting CSS properties. jQuery("#someid").css("color","red"); //Setting inner HTML. jQuery("#someid").html("<p>Just some text.<p/>"); //Call html() without parameters to get the html instead. //Setting inner text. jQuery("#someid").text("Some other text."); //Call text() without parameters to get the text instead.