Before writing any jQuery code, jQuery must be added to the website.
If jQuery is not included properly, the browser will not recognize jQuery functions and the code will not work.
There are two main ways to add jQuery to a website:
- Using jQuery CDN
- Using a local jQuery file
Both methods are commonly used in real-world projects.
This is a basic HTML structure.
jQuery is usually added inside the head section or just before the closing body tag.
The above code loads jQuery from an online server called CDN.
CDN stands for Content Delivery Network. It allows faster loading and does not require downloading files.
Once jQuery is added, you can start writing jQuery code inside a script tag.
Always make sure jQuery is loaded before your custom script.
The document ready function ensures that HTML elements are loaded before jQuery runs.
This is a standard practice followed in professional projects.
Important Notes:
- jQuery must be included before using $
- Always load jQuery before your custom script
- CDN method is preferred for small projects
- Local file method is preferred for offline projects