Method
Description
Example
data()
Retrieve the value of a data attribute for the first element in the set of matched elements.
$("#element").data("key");
each()
Iterate over a jQuery object, executing a function for each matched element.
$("li").each(function(index, element) { // Your code here});
get()
Retrieve the DOM elements matched by the jQuery object as an array.
var elements = $("p").get();
index()
Search for a given element from among the matched elements and returns its index.
var index = $("#item").index();
$.noConflict()
Relinquish jQuery's control of the `$` variable.
var jq = $.noConflict();
$.param()
Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.
var queryString = $.param({ key1: "value1", key2: "value2" });
removeData()
Remove a previously-stored piece of data.
$("#element").removeData("key");
size()
Return the number of elements in the jQuery object.
var count = $("div").size();
toArray()
Retrieve all the DOM elements contained in the jQuery set as an array.
var elementsArray = $("p").toArray();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Utility Methods Example</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<style>
div {
padding: 10px;
margin: 5px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="exampleElement" data-info="Sample Data">This is an example element.</div>
<script>
// Using data() method to retrieve and set data
var infoData = $("#exampleElement").data("info");
console.log("Data retrieved using data():", infoData);
// Using each() method to iterate over elements
$("div").each(function(index, element) {
console.log("Element at index", index, ":", element);
});
// Using get() method to retrieve DOM elements as an array
var elementsArray = $("div").get();
console.log("Array of DOM elements:", elementsArray);
// Using index() method to get the index of an element
var elementIndex = $("#exampleElement").index("div");
console.log("Index of #exampleElement among div elements:", elementIndex);
// Using $.noConflict() to release the $ variable
var jq = $.noConflict();
jq(document).ready(function() {
jq("div").css("background-color", "lightyellow");
});
// Using $.param() to serialize data for URL query string
var params = { name: "John", age: 30, city: "New York" };
var queryString = $.param(params);
console.log("Serialized URL query string:", queryString);
// Using removeData() to remove stored data
$("#exampleElement").removeData("info");
// Using size() to get the number of matched elements
var elementSize = $("div").size();
console.log("Number of div elements:", elementSize);
// Using toArray() to convert the set of matched elements to an array
var elementsAsArray = $("div").toArray();
console.log("Matched elements as an array:", elementsAsArray);
</script>
</body>
</html>
The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.