Appends content at the end of selected elements.
$(document).ready(function () {
$("#myDiv").append("<p>This is some appended content.</p>");
});
Appends an HTML element at the end of selected element.
$(document).ready(function () {
$("<span>This is appended.</span>").appendTo("p");
});
Sets or returns the innerHTML content of selected element.
$(document).ready(function () {
$("#myDiv").html("<p>New content set with .html() method.</p>");
});
Inserts HTML elements or content at the beginning of selected element.
$(document).ready(function () {
$("<h2>This is prepended.</h2>").prependTo("#myDiv");
});
Sets or returns the text content of the element.
$(document).ready(function () {
$("p").text("New text content set with .text() method.");
});
Creates a copy of selected elements including its children.
$(document).ready(function () {
var $clonedList = $("#myList").clone();
$clonedList.appendTo("#newDiv");
});
Inserts HTML content before a specified element.
$(document).ready(function () {
$("<p>This is inserted before the heading.</p>").insertBefore("h2");
});
Inserts HTML content after a specified element.
$(document).ready(function () {
$("<p>This is inserted after the heading.</p>").insertAfter("h2");
});
Removes the selected elements from the DOM tree.
$(document).ready(function () {
var $myDiv = $("#myDiv");
$("#detachButton").click(function () {
$myDiv.detach();
});
$("#reattachButton").click(function () {
$myDiv.appendTo("body");
});
});
Removes all child nodes and their content for selected elements.
$(document).ready(function () {
$("#emptyButton").click(function () {
$("#myDiv").empty();
});
});
Removes all the selected elements including the text.
$(document).ready(function () {
$("#removeButton").click(function () {
$("p").remove();
});
});
Replaces the selected element with new content.
$(document).ready(function () {
$("#replaceButton").click(function () {
$("p").replaceWith("<h2>New heading replacing the paragraph.</h2>");
});
});
Wraps a specified element around all selected elements.
$(document).ready(function () {
$("p").wrapAll("<div class='wrapper'></div>");
});
Gets the value of an attribute for the first element in the set of matched elements.
$(document).ready(function () {
var srcValue = $("img").attr("src");
console.log("Image source:", srcValue);
});
Gets the innerHTML content of the first matched element.
$(document).ready(function () {
var htmlContent = $("#myDiv").html();
console.log("HTML content:", htmlContent);
});
Gets the current value of the first element in the set of matched elements.
$(document).ready(function () {
var inputValue = $("input").val();
console.log("Input value:", inputValue);
});
This method retrieves all the direct child elements of a selected element.
$("ul").children("li").css("color", "blue");
The .next() method gets the next sibling element of the selected element.
$("h2").next("p").addClass("highlight");
This method fetches the closest ancestor of the selected element that matches the provided selector.
$("a").closest("div").addClass("active");
The .parent() method selects the direct parent element of the chosen element.
$("span").parent().addClass("highlight");
.prevUntil():
.prevUntil() selects all sibling elements that come before the selected element up to a specified element.
$("h3").prevUntil("h1").addClass("background");
The .siblings() method retrieves all sibling elements of the chosen element.
$("li.selected").siblings().removeClass("selected");
This method selects the first element from a set of elements.
Example
$("p").first().addClass("first-paragraph");
The .last() method selects the last element from a set of elements.
$("div.item").last().addClass("last-item");
.is():
.is() checks if the selected element matches a given selector.
if ($("input").is(":checked")) {
console.log("Checkbox is checked.");
}
The .map() method transforms each element in an array and returns a new array.
const lengths = $("p").map(function() {
return $(this).text().length;
}).get();
This method narrows down elements based on certain criteria and returns the matching elements.
$("li").filter(".important").addClass("highlight");
The .not() method selects all elements except those that match the specified selector.
$("div").not(".special").addClass("regular");
This method adds the previously selected elements to the current set.
$("li").next().andSelf().addClass("highlight");
The .each() method applies a function to each element in the selection.
$("ul li").each(function(index) {
$(this).text("Item " + (index + 1));
});
This method retrieves all descendant elements of the selected element.
$("div.container").find("p").addClass("inside-container");