EzDevInfo.com

jquery interview questions

Top jquery frequently asked interview questions

Add table row in jQuery

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)

Is there an "exists" function for jQuery?

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)

Advertisements

Get selected text from a drop-down list (select box) using jQuery

How can I get a drop-down list selected text in jQuery, not using the selected value?


Source: (StackOverflow)

How can I make a page redirect using jQuery?

How can I redirect the user from one page to another using jQuery?


Source: (StackOverflow)

Checking if an element is hidden

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)

Check checkbox checked property

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)

Checking a checkbox with jQuery?

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)

$(this) selector and children?

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)

Disable/enable an input with jQuery?

$input.disabled = true;

or

$input.disabled = "disabled";

Which is the standard way? And, conversely, how do you enable a disabled input?


Source: (StackOverflow)

How can I select an element with multiple classes?

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)

How can I refresh a page with jQuery?

How can I refresh a page with jQuery?


Source: (StackOverflow)

Abort Ajax requests using jQuery

Using jQuery, how can I cancel/abort an Ajax request that I have not yet received the response from?


Source: (StackOverflow)

How to detect a click outside an element?

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)

How do I return the response from an asynchronous call?

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)

jQuery document.createElement equivalent?

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)