1
Identify the error in the following jQuery code snippet:
$(".btn").click(function() { $(this).toggleClass("active"); $(this).siblings().removeClass("active"); });
View Answer
There is no error.
.toggleClass()
should be replaced with.addClass()
.
.siblings()
should be replaced with.siblings(".btn")
.
.removeClass("active")
should be replaced with.removeClass()
.
2
What is wrong with the following jQuery AJAX request?
$.get("data.json", function(data) { console.log(data); }).fail(function(error) { console.error("Error:", error); });
View Answer
There is no error.
.get()
should be replaced with.ajax()
.
.fail()
should be replaced with.error()
.
.fail()
should be replaced with.catch()
.
3
What mistake is present in the following jQuery animation code?
$("#box").animate({ left: '250px', top: '+=150px' }, 1000, function() { console.log("Animation complete"); });
View Answer
There is no error.
top: '+=150px'
should be replaced withbottom: '+=150px'
.The duration
1000
should be in quotes:"1000"
.The callback function should be inside quotes:
function() { ... }
.
4
Identify the mistake in the following event delegation setup:
$("ul").on("click", "li", function() { $(this).addClass("selected"); $(this).siblings().removeClass("selected"); });
View Answer
There is no error
"li"
should be replaced with"ul li"
.
.siblings()
should be replaced with.children()
.
.addClass("selected")
should be replaced with.toggleClass("selected")
.
5
What is the issue with the following jQuery selector?
$("input[type='text']").val("Hello World!");
View Answer
There is no error.
"text"
should be replaced with 'text'
.val("Hello World!")
should be replaced with.text("Hello World!")
.val("Hello World!")
should be replaced with.html("Hello World!")