jQuery
jQuery
jQuery is a popular and widely used JavaScript library that simplifies client-side scripting in web development.
It was designed to make it easier for developers to interact with HTML documents, handle events, create animations, and
perform various other tasks in a more efficient and cross-browser compatible way.
Selector Description
We can also define a custom function and then apply the advance selector accordingly.
Best approach to Implement jQuery in HMTL
<!DOCTYPE html>
<html>
<head>
<script src="~/js/script.js"></script>
</head>
<body>
<button id="myButton"> Click Me </button>
<div id="myDiv">This is a div element.</div>
</body>
</html>
3. Write jQuery Code:
$(document).ready(function () {
$("#myButton").click(function () {
$("#myDiv").text("Button clicked!");
});
});
5. Cashing : External JavaScript files is cached by browsers, leading to faster page loading on subsequent visits, reducing server requests
Mouse Click
1. click()
<script>
$(document).ready(function () {
$("#box").click(function () {
// Change the background color to red on a single click
$("#box").css("background-color", "red");
});
});
</script>
2. dbclick()
<script>
$(document).ready(function () {
$("#box").dblclick(function () {
// Change the background color to blue on a double click
$(this).css("background-color", "blue"); //Event Binding property of jquery
});
});
</script>
3. contextmenu()
<script>
$(document).ready(function () {
$("#box").contextmenu(function (e) {
// Prevent the default context menu
e.preventDefault();
// Change the background color to green on right-click
$(this).css("background-color", "green");
});
});
</script>
4. mouseenter()
<script>
$(document).ready(function () {
$("#box").mouseenter(function () {
// Change the background color to orange on mouse enter
$(this).css("background-color", "orange");
});
});
</script>
4. mouseleave()
<script>
$(document).ready(function () {
$("#box").mouseleave(function () {
// Change the background color to purple on mouse leave
$(this).css("background-color", "purple");
});
});
</script>
"mouseover":function () {
$(this).css("background-color", "orange");
},
"mouseout": function () {
$(this).css("background-color", "yellow");
},
"click":function () {
$("#box").attr("data-custom", "custom-value");
},
“click":function () {
$("#box").val("New Value");
}
});
});
Remove() and Empty() method
<script>
$(document).ready(function () {
$("#Remove").click(function () {
$("#box").remove();
});
$("#Empty").click(function () {
$("#box h2").empty();
});
});
</script>
Clone() method
<script>
$(document).ready(function () {
// Clone the h2 element and prepend it to #box2
var h2Clone = $("#box1 h2").clone();
h2Clone.prependTo("#box2");
● appendTo() adds the content as the last child of the selected element
● prependTo() adds the content as the first child of the selected element
Method Chaining
<script> <script>
$(document).ready(function () { $(document).ready(function () {
$("#clickbtn").click(function () { $("#clickbtn").click(function () {
$("#box1").animate({ $("#box1").animate({
</script>
jQuery DOM Traversing Methods
• Ancestors Methods
• Descendants Methods
• Siblings Methods
• Filtering Methods
Ancestors Methods
grandparent
great-grandparent
parent
Child
There are 5 types of Ancestors method
$(document).ready(function(){
1. parent() $(‘#child-c’).parent().css(‘background’, ‘red’);
});
$(document).ready(function(){
$(document).ready(function(){
$(‘#child-c’).parents().css(‘background’, ‘red’);
2. parents() ;
$(‘#child-c’).parents(‘#outer’);
});
});
Id=o$(document).ready(function(){
$(‘#child-c’).parentsUntil(‘#outer’).css({"color": "red", "border": "2px solid red"});
});
3. parentsUntil() uter2
Id=outer2
Id=outer
Id=inner
Id=child-head
Id-child-c
$(document).ready(function(){
4. closet() $(‘#child-c’).closet(‘span’).css(‘background’, ‘red’);
});
$(document).ready(function(){
5. offsetParent() $(‘#child-c’).offsetparent().css(background’, ‘red’);
});
Id=outer2
Id=inner
Id=child-head
Id-child-c
Descendants Methods
grandparent
great-grandparent
parent
Child
$(document).ready(function(){
2. find() $(‘#inner’).find(”child-
head").css(‘background’, ‘red’);
});
Id=outer2
Id=outer
Id=inner
Id=child-head
Id-child-c
Thank You !