Jquery Interview Questions and Answers For Experienced and Freshers
Jquery Interview Questions and Answers For Experienced and Freshers
jQuery is one of the most popular JavaScript framework for web development. It is mainly
used language in web development companies. If you are searching for a job as a web
developer, you should know the jQuery framework.
What is jQuery?
It is used for :
Document traversing
Event handling
Animations
AJAX interaction
Cool plugins
AJAX support
DOM selection
Changing CSS
Event Handling
Quickly implementation of animations and effects.
CSS Selector
XPath Selector
Custom Selector
Ans. jQuery selector is used to selecting one or more HTML elements from the webpage.
It always starts with $() symbol.
Jquery supports CSS, Xpath, and custom selectors.
Ans.
Select by tag name: In this selection, HTML elements are selected according to their tag
names.
Example:
$(h2)
Select by id: In this selection, HTML elements are selected according to their id.
Example:
$(“#id1”)
Select by class: In this selection, HTML elements are selected according to their class names.
Example:
$(“.c1”)
body.onload() document.ready()
jQuery has more than one copy of
jQuery has only one copy of body.onload()
document.ready()
It is called when all elements such as Images, DOM,
It is called when only DOM is loaded.
and Resources are loaded.
Ans. In jQuery width() and CSS(‘width’), both are used to change the width of an element.
In CSS(‘width’), we have to specify the width value in px, but in width(), there is no need to
specify the width value in px.
Example:
$(‘#id1’).width(100);
$(‘#id2’).css(‘width’ , 100px);
When we want to get the width value of elements, then the first element will return the 100 as
value. And the second element will return 100px as the value because of CSS(‘width’)
function.
Ans. The bind() method only attach the event with the elements which are attached before the
DOM is loaded.
Example:
<script>
$("p").bind("click", function(){
alert("I am was clicked.");
});
</script>
The above code will attach the function code with only <p> tag in HTML code.
Ans. The live() method is used to attach an event with elements of two types first, which is
attached before the DOM is loaded and second for future elements.
The live() function doesn’t work in chaining. It can only apply to one selector.
Example:
$(document).ready(function(){
$("button").live("click", function(){
$("p").slideToggle();
});
});
</script>
The above Jquery will toggle the <p> tag when the button is clicked.
Ans. The delegate() method is used to attach an event with the element of two types first,
which is attached before the DOM the loaded and second for future elements. It can work in
the chaining.
Example:
<script>
$(document).ready(function(){
$("div").delegate("p", "click",function(){
$("p").css("font-size", "30px");
});
});
</script>
The above code work in chaining as selects the <p> tag inside the <div> tag and attach event
using delegate() method.
When you click on the <p> tag, it will change the font size to 30px.
Ans. The param() is used to arrange the data of an array or object in a serial manner. This
feature is useful while making an AJAX request.
Syntax:
Example:
<script>
$(document).ready(function(){
personObj = new Object();
personObj.firstname = "John";
personObj.lastname = "Doe";
personObj.age = 20;
personObj.eyecolor = "black";
$("button").click(function(){
$("div").text($.param(personObj));
});
});
</script>
The above code puts the serialize object inside the div element when the button is clicked.
Output:
firstname=John&lastname=Doe&age=20&eyecolor=black
Ans.
Ans. The dough is used for cookie-related operations. It is an easy and powerful feature of
jQuery.
1. Create a cookie
Syntax: $.dough(“cookie_name”, “cookie_value”);
2. Read Cookie
Syntax: $.dough(“cookie_name”);
3. Delete cookie
Syntax: $.dough(“cookie_name”, “remove”);
Ans. The this and $(this) both are used to reference the element in HTML. This is the
traditional method, and $(this) is then used to point a jQuery object on which the jQuery
function will be applied.
1. $.ajax() - It is the basic function of AJAX. It is used to send the ajax request.
2. $.ajaxSetup() - It is used to set the options for ajax calls.
3. $.getJSON - It accepts the URL and sends the request to that URL.
Ans. The empty() method removes all child elements of the matched element.
Syntax:
$(selector_name).empty();
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").empty();
});
});
</script>
The above code will remove all the content inside the <div> tag when the button is clicked.
The remove() method is used for removing only matched elements. It will remove the data
inside the matched element.
Syntax:
$(selector_name).remove();
Example:
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").remove();
});
});
</script>
The above code removes the tag having #div1 as an id and also removes the data inside it
when the buttons will be clicked.
Ans. The detach() method is as same as remove() method, but the only difference is that it
will not remove the data inside the selected element. It keeps a copy of the detached elements
data.
Syntax:
$(selector_name).detach();
Example:
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").detach();
});
});
</script>
What are the parameters used for the jQuery AJAX method?
Ans. The following parameters are used in the jQuery AJAX method.
Conclusion
That is all for jQuery Interview Questions for experienced, and if you are a fresher, don’t
worry if you were not able to answer some tricky questions. I am sure you will feel confident
after preparing for the Jquery interview using this series of questions.