smallseo.info

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)

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)

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)

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)

jQuery's jquery-1.10.2.min.map is triggering a 404 (Not Found)

I'm seeing error messages about a file, min.map, being not found:

GET jQuery's jquery-1.10.2.min.map is triggering a 404 (Not Found)


Screenshot

enter image description here

Where is this coming from?


Source: (StackOverflow)

Is Safari on iOS 6 caching $.ajax results?

Since the upgrade to iOS 6, we are seeing Safari's web view take the liberty of caching $.ajax calls. This is in the context of a PhoneGap application so it is using the Safari WebView. Our $.ajax calls are POST methods and we have cache set to false {cache:false}, but still this is happening. We tried manually adding a TimeStamp to the headers but it did not help.

We did more research and found that Safari is only returning cached results for web services that have a function signature that is static and does not change from call to call. For instance, imagine a function called something like:

getNewRecordID(intRecordType)

This function receives the same input parameters over and over again, but the data it returns should be different every time.

Must be in Apple's haste to make iOS 6 zip along impressively they got too happy with the cache settings. Has anyone else seen this behavior on iOS 6? If so, what exactly is causing it?


The workaround that we found was to modify the function signature to be something like this:

getNewRecordID(intRecordType, strTimestamp)

and then always pass in a TimeStamp parameter as well, and just discard that value on the server side. This works around the issue. I hope this helps some other poor soul who spends 15 hours on this issue like I did!


Source: (StackOverflow)

$(document).ready equivalent without jQuery

I have a script that uses $(document).ready, but doesn't use anything else from jQuery. I'd like to lighten it up by removing the jQuery dependency.

How can I implement my own $(document).ready functionality without using jQuery? I know that using window.onload will not be the same, as window.onload fires after all images, frames etc have been loaded.


Source: (StackOverflow)