$.ajax({
url: "wrong_url.json",
method: "GET",
error: function(xhr, status, error){
console.log("Error: " + error);
}
});
Category: Interview Questions
-
Solve errors in jQuery AJAX?
-
prop() and attr()?
.attr()
→ Gets/sets HTML attributes..prop()
→ Gets/sets DOM properties.
Example:
$("#check").attr("checked"); // may return "checked" $("#check").prop("checked"); // returns true/false
-
element is hidden in jQuery?
if($("#box").is(":hidden")){ console.log("Box is hidden"); }
-
event delegation in jQuery?
Event delegation means attaching an event to a parent element so that future child elements also inherit it.Example:
$(document).on("click", "button.dynamic", function(){ alert("New button clicked!"); });
Useful when elements are added dynamically.
-
How do you stop an animation in jQuery?
Use
.stop()
method.$("#box").slideDown(5000); $("#stopBtn").click(function(){ $("#box").stop(); // stops animation });
-
How do you make an AJAX call in jQuery?
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);
-
What is chaining in jQuery?
Chaining means calling multiple methods on the same element in one line.
$("#box").css("color", "red").slideUp(1000).slideDown(1000);
Improves performance and readability.
-
How do you show and hide elements in jQuery?
$("#id").show();
→ Show element$("#id").hide();
→ Hide element$("#id").toggle();
→ Toggle between show/hide
-
Difference between .on() and .bind()?
.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!"); });
-
How do you select elements in jQuery?
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>