Basic jQuery Syntax and Selectors: Navigating the DOM Like a Pro
🧱 DOM Manipulation with jQuery
jQuery simplifies DOM manipulation using a concise syntax:
$(selector).action()
- The
$
function is shorthand forjQuery()
🔍 Common Selectors
$('p')
→ Targets all<p>
tags$('#id')
→ Targets element with a specific ID$('.class')
→ Targets elements with a specific class
✏️ Example: Change Paragraph Text
$(document).ready(function(){
$('p').text('New paragraph content');
});
✅ This code waits until the DOM is fully loaded, ensuring elements are accessible before manipulation.
🎯 Advanced Selection
jQuery selectors can be combined and refined for precise targeting:
$('div > p.highlight')
→ Paragraphs with classhighlight
directly inside<div>
$('ul li:first')
→ First<li>
in a list
Using jQuery's selector engine, developers can easily and efficiently manipulate document elements with minimal code.