How to execute jQuery Code ?
In this article, we will see how to execute the jQuery code for our webpage. Executing the jQuery code is quite easy, just need to take care of few steps, and then we are good to go. Before jump to the main topic, we will first see how we can add & use jQuery in our webpage. There are two ways to add jQuery to our webpage/website:
Let’s head to our first method ie, downloading the library from the website.
Method 1: Download jQuery library
Declare the jQuery file path in the <script> tag inside the <head> tag section.
Example 1: Create an HTML file & add below code into your HTML file.
In the code above, we have added the jQuery file using a <script> tag. Basically, that’s all we need to do to add jQuery to our webpage. Now we can write some jQuery code and use it.
Example 2: In this example, we will write some jQuery code and see how we can execute it.
Output: In this example, we have written code to change the text color. To run this code, open this HTML file in the browser.
Let’s head to our next method of adding the jQuery ie, by using the jQuery CDN.
Method 2: Using jQuery CDN link
This method of using jQuery is quite easy, we don’t need to download anything, but let’s have brief information about CDN. CDN stands for content delivery network, basically, it is a worldwide distributed group of servers for different kinds of web services that provides the internet content with fast delivery. For example, the jQuery CDN has all the necessary information related to jQuery and any user can directly access it from their servers anytime. This method is quite reliable and updated, in order to work with CDN of jQuery or any other library, we must be connected with the network. Now let’s see how we can use it. We have to add <script> tag in our HTML file as shown below. Paste the following CDN code in the head section in HTML.
jQuery CDN Link:
<script src=”https://code.jquery.com/jquery-3.6.0.min.js” integrity=”sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=” crossorigin=”anonymous”></script>
Example: We will use example 2 here with the jQuery CDN link in order to see the change.
We have added the CDN code, now we are ready to work with jQuery once again. So, this is how we can execute our jQuery with some easy methods.