jquery interview questions
Top jquery frequently asked interview questions
What is the best method in jQuery to add an additional row to a table as the last row?
Is this acceptable?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?
Source: (StackOverflow)
How can I check the existence of an element in jQuery?
The current code that I have is this:
if ($(selector).length>0) {
// Do something
}
Is there is a more elegant way to approach this? Perhaps a plugin or a function?
Source: (StackOverflow)
In jQuery, it is possible to toggle the visibility of an element, using the functions .hide()
, .show()
or .toggle()
.
Using jQuery, how would you test if an element is visible or hidden?
Source: (StackOverflow)
I need to check the checked
property of a checkbox and perform an action based on the checked property using jQuery.
For example, if the age checkbox is checked, then I need to show a textbox to enter age, else hide the textbox.
But the following code returns false
by default:
if($('#isAgeSelected').attr('checked')) {
$("#txtAge").show();
} else {
$("#txtAge").hide();
}
How do I successfully query the checked
property?
Source: (StackOverflow)
I'd like to do something like this to tick a checkbox
using jQuery:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
Does such a thing exist?
Source: (StackOverflow)
I have a layout similar to this:
<div id="..."><img src="..."></div>
and would like to use a jQuery selector to select the child img
inside the div
on click.
To get the div
, I've got this selector:
$(this)
How can I get the child img
using a selector?
Source: (StackOverflow)
$input.disabled = true;
or
$input.disabled = "disabled";
Which is the standard way? And, conversely, how do you enable a disabled input?
Source: (StackOverflow)
I want to select all the elements that have the two classes a
and b
.
<element class="a b">
So, only the elements that have both classes.
When I use $(".a, .b")
it gives me the union, but I want the intersection.
Source: (StackOverflow)
Using jQuery, how can I cancel/abort an Ajax request that I have not yet received the response from?
Source: (StackOverflow)
I have some HTML menus, which I show completely when a user clicks on the head of these menus. I would like to hide these elements when the user clicks outside the menus' area.
Is something like this possible with jQuery?
$("#menuscontainer").clickOutsideThisElement(function() {
// hide the menus
});
Source: (StackOverflow)
I have a function foo
which makes an Ajax request. How can I return the response from foo
?
I tried to return the value from the success
callback as well as assigning the response to a local variable inside the function and return that one, but none of those ways actually return the response.
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- I tried that one as well
}
});
return result;
}
var result = foo(); // It always ends up being `undefined`.
Source: (StackOverflow)
I'm refactoring some old JavaScript code and there's a lot of DOM manipulation going on.
var d = document;
var odv = d.createElement("div");
odv.style.display = "none";
this.OuterDiv = odv;
var t = d.createElement("table");
t.cellSpacing = 0;
t.className = "text";
odv.appendChild(t);
I would like to know if there is a better way to do this using jQuery. I've been experimenting with:
var odv = $.create("div");
$.append(odv);
// And many more
But I'm not sure if this is any better.
Source: (StackOverflow)