jQuery is a tool that makes it easier to work with JavaScript by offering simple methods for selecting and changing parts of a web page. This means you can add interactivity and make changes to your website using fewer lines of code.
// In regular JavaScript const menu = document.getElementById('menu'); const closeMenuButton = document.getElementById('close-menu-button'); closeMenuButton.addEventListener('click', () => { menu.style.display = 'none'; }); // With jQuery $('#close-menu-button').on('click', () => { $('#menu').hide(); });
JavaScript code needs to run only after the web page has fully loaded. Using jQuery’s `$(document).ready()` function ensures that your code runs only after the page is ready, avoiding errors and unexpected behavior.
$(document).ready(function() { alert('The page is fully loaded!'); });
In jQuery, variables that store jQuery objects usually start with a `$` symbol. This helps to easily identify which variables are related to jQuery.
// A variable for a jQuery object const $myButton = $('#my-button');
To use jQuery, include it in your HTML file by adding a script tag. You can use a link to a jQuery file hosted online.
The `.slideToggle()` method makes an element slide up or down, switching between showing and hiding. It’s a simple way to create smooth animations.
// Clicking '#menu-button' will make '#menu' slide up or down. $('#menu-button').on('click', () => { $('#menu').slideToggle(); });
The `.fadeIn()` method makes an element gradually appear. It’s useful for smoothly revealing hidden content.
// Clicking '#menu-button' will make '#menu' fade in. $('#menu-button').on('click', () => { $('#menu').fadeIn(); });
The `.fadeOut()` method makes an element gradually disappear. This is handy for smoothly hiding content.
// Clicking '#menu-button' will make '#menu' fade out. $('#menu-button').on('click', () => { $('#menu').fadeOut(); });
The `.show()` method instantly makes an element visible. This is useful when you need to reveal something without any animation.
// Clicking '#show-menu-button' will instantly show '#menu'. $('#show-menu-button').on('click', () => { $('#menu').show(); });
The `.toggle()` method alternates between showing and hiding an element each time it is triggered. It’s a quick way to toggle visibility.
// Clicking '#menu-button' will toggle '#menu' between visible and hidden. $('#menu-button').on('click', () => { $('#menu').toggle(); });
The `.fadeToggle()` method alternates between fading an element in and out. It’s useful for smooth transitions between visible and hidden states.
// Clicking '#menu-button' will fade '#menu' in or out. $('#menu-button').on('click', () => { $('#menu').fadeToggle(); });
The `.slideUp()` method makes an element gradually slide up and disappear. It’s a smooth way to hide elements with a sliding motion.
// Clicking '#menu-button' will slide '#menu' up and hide it. $('#menu-button').on('click', () => { $('#menu').slideUp(500); });
The `.hide()` method instantly hides an element. It’s useful for quickly removing elements from view without animation.
// Clicking '#hide-menu-button' will instantly hide '#menu'. $('#hide-menu-button').on('click', () => { $('#menu').hide(); });
The `.slideDown()` method makes an element gradually slide down and appear. It’s a smooth way to show elements with a sliding effect.
// Clicking '#menu-button' will make '#menu' slide down and appear. $('#menu-button').on('click', () => { $('#menu').slideDown(); });
The `.on()` method in jQuery allows you to attach event listeners to elements, such as clicks or key presses, and define what should happen when those events occur.
// Handle a click event $('#menu-button').on('click', () => { $('#menu').show(); }); // Handle a keyup event $('#textbox').on('keyup', () => { $('#menu').show(); }); // Handle a scroll event $('#menu-button').on('scroll', () => { $('#menu').show(); });
When an event occurs, jQuery provides an event object to the event handler. This object contains useful information about the event, like the type of event and the element that triggered it.
// Hides '#menu' when it is clicked. $('#menu').on('click', event => { $(event.currentTarget).hide(); });
The `event.currentTarget` property in jQuery refers to the specific element that triggered the event, even if multiple elements share the same event listener.
// Hides the specific '.blue-button' that was clicked. $('.blue-button').on('click', event => { $(event.currentTarget).hide(); });
You can chain multiple `.on()` methods together to handle different events on the same element. This allows for handling multiple interactions in a concise manner.
// Handling mouseenter and mouseleave events on '#menu-button'. $('#menu-button').on('mouseenter', () => { $('#menu').show(); }).on('mouseleave', () => { $('#menu').hide(); });
The `.on()` method attaches event handlers to elements. It requires two parameters: the type of event (like 'click') and the function to execute when the event occurs.
// Clicking '#login' shows '#login-form'. $('#login').on('click', () => { $('#login-form').show(); });
The `.animate()` method allows you to create animations by changing CSS properties over time.
// Hovering over '.tile' animates its text color and background color. $('.tile').on('mouseenter', () => { $('.tile-text').animate({ color: '#FFFFFF', backgroundColor: '#000000' }, 300); // Animation lasts 300 milliseconds });
The `.css()` method sets or changes CSS properties for selected elements. It can be used to modify styles directly.
// Changing the font size of '.tile-text'. $('.tile-text').css('font-size', '20px');
Inline styles can be set using jQuery’s `.css()` method, which directly affects an element's style attributes.
// Change the background color of '#menu'. $('#menu').css('background-color', 'red');
You can use `.css()` to retrieve the current value of a CSS property. This is useful for checking and manipulating styles based on current values.
// Getting the current color of '.tile-text'. const color = $('.tile-text').css('color'); console.log(color);
Use the `#` symbol followed by the ID name to select a specific element by its ID. This is a quick way to target a single element.
// Selecting an element with ID 'menu'. $('#menu').show();
Use the `.` symbol followed by the class name to select elements with a specific class. This targets multiple elements that share the same class.
// Selecting all elements with the class 'tile'. $('.tile').hide();
Use the tag name directly to select all elements of a specific type. This is useful for targeting elements like `
` or `
// Selecting all paragraph elements. $('p').css('color', 'blue');
Use the `.find()` method to select child elements within a parent element. This helps narrow down the scope of selection to specific parts of the DOM.
// Finding child elements with class 'tile-text' inside '#menu'. $('#menu').find('.tile-text').show();
The `.parent()` method allows you to select the parent element of a specified child. This is useful for traversing up the DOM hierarchy.
// Selecting the parent of '#tile'. $('#tile').parent().css('background-color', 'yellow');
Use the space between selectors to select descendant elements. This targets elements nested within a specific parent.
// Selecting '.tile-text' inside '#menu'. $('#menu .tile-text').hide();
Use the `+` symbol to select the next sibling element or `~` to select all following siblings. This is useful for targeting elements at the same level in the DOM.
// Selecting the next sibling of '#current' with class 'next-sibling'. $('#current').next('.next-sibling').show();
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!