Basic JQuery Syntax
Basic JQuery Syntax
$(selector).action()
2. Manipulating Content
3. Adding/Removing Elements
// Remove an element
$('#myElement').remove()
4. Styling Elements
$('#myElement').css('color', 'blue')
5. Handling Events
$('#myButton').click(function() {
alert('Button clicked!')
})
$('#myElement').hover(
function() { $(this).css('background-color', 'yellow') }, // Mouse enter
function() { $(this).css('background-color', '') } // Mouse leave
)
6. Animations
// Fade in an element
$('#myElement').fadeIn()
// Slide up an element
$('#myElement').slideUp()
$('#myElement').animate({
'width': '200px',
'height': '200px'
}, 1000) // duration in milliseconds
7. AJAX Requests
$.get('url', function(data) {
console.log(data)
})
Chaining
jQuery allows for method chaining, where multiple methods can be called on the same
jQuery object in a single line:
$('#myElement').css('color', 'blue').slideUp().fadeIn()
Utility Functions
jQuery also provides utility functions that are not directly related to DOM
manipulation: