There are a number of jQuery methods (actions) that can be used to manipulate your document. Here are some of the most common.
The following are merely quick examples of the method’s use. A complete list of methods (actions) can be found here along with more detailed descriptions of their intended use and syntax.
addClass() Method
The addClass() method adds one or more class names to the selected elements.
// add a .content class to all paragraphs
$('p').addClass('content');
attr() Method
The attr() method sets attributes and values of the selected elements.
// adds and sets the width for images
$('img').attr('width', '80');
click() Method
The click event occurs when an HTML element is clicked.
// provide an alert when a div is clicked by the user
$('div').click(function() {
alert('The element has been clicked.');
});
css() Method
The css() method sets one or more style properties for the selected elements.
// set color property for all paragraphs
$('p').css('color', 'red');
html() Method
The html() method sets the content (innerHTML) of the selected elements.
// sets the content of all paragraphs to say Hello World!
$('p').html('Hello world!');
on() Method
The on() method attaches one or more event handlers to the selected elements and child elements.
// when the user "clicks" on a div, provide an alert
$('div').on('click', function() {
alert('The element was clicked.');
});
removeAttr() Method
The removeAttr() method removes one or more attributes from the selected elements.
// removes the color attribute from all paragraph elements
$('p').removeAttr('color');
removeClass() Method
The removeClass() method removes one or more class names from the selected elements.
// removes the .section class from all paragraph elements
$('p').removeClass('section');
text() Method
The text() method sets the text content of the selected elements, overwriting the content of ALL matched elements. This only replaces the text inside the element and does NOT remove the elements tags or attributes.
// replace the text within all paragraph elements
$('p').text('Hello world');
val() Method
The val() method sets the value attribute of the selected elements.
// sets the value for all form input fields that are text
$('input:text').val('Hello World');
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.
Why 1SMARTchicken?
See More →
Thanks for your support!
jQuery Books
All are affiliate links. Thank you for your support!
jQUERY NOTES:
- To use jQuery on your site, it must first be downloaded from the official jQuery site and linked to in your document
head
, or linked to via a CDN in your documenthead
- It is generally good practice to place your jQuery code/function inside the document load function so that the action takes place ONLY after the document has finished loading
- When using jQuery, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
Feedback
If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.