Answer:
$.ajax({
url: "data.json",
method: "GET",
success: function(response){
console.log(response);
}
});
Also simpler shortcuts:
$("#div").load("file.txt");
$.get("file.txt", callback);
$.post("server.php", data, callback);
Answer:
$.ajax({
url: "data.json",
method: "GET",
success: function(response){
console.log(response);
}
});
Also simpler shortcuts:
$("#div").load("file.txt");
$.get("file.txt", callback);
$.post("server.php", data, callback);
Chaining means calling multiple methods on the same element in one line.
$("#box").css("color", "red").slideUp(1000).slideDown(1000);
Improves performance and readability.
$("#id").show();
→ Show element$("#id").hide();
→ Hide element$("#id").toggle();
→ Toggle between show/hide.bind()
→ Older method for event binding (not recommended now)..on()
→ Modern, more powerful. Can bind multiple events and works with dynamically added elements.Example:
// Old
$("#btn").bind("click", function(){ alert("Clicked!"); });
// New
$("#btn").on("click", function(){ alert("Clicked!"); });
Selectors in jQuery are similar to CSS selectors.
$("#id")
→ Select element by ID$(".class")
→ Select by class$("p")
→ Select all <p>
tags$("p:first")
→ Select first <p>
$(document).ready()
→ Runs when DOM is ready (faster).window.onload
→ Runs when entire page including images & resources is loaded (slower).// JavaScript
document.getElementById("box").style.display = "none";
// jQuery
$("#box").hide();
jQuery is a fast, lightweight JavaScript library that simplifies tasks like:
It allows you to write less code to achieve more (e.g., $("#id").hide()
instead of long JavaScript code).
<p id="delayText">Watch me hide and show with delay!</p>
<button id="delayBtn">Start</button>
<script>
$(document).ready(function(){
$("#delayBtn").click(function(){
$("#delayText").fadeOut(1000).delay(2000).fadeIn(1000);
});
});
</script>
.delay()
pauses the animation chain.