jQuery JavaScript Library jQuery jquery: the write less, do more, javascript library

Checking a checkbox with jQuery?

I'd like to do something like this to tick a checkbox using jQuery:




Does such a thing exist?

Source: (StackOverflow)

How to 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;

        url: '...',
        success: function(response) {
            result = response;
            // return response; // <- tried that one as well

    return result;

var result = foo(); // always ends up being `undefined`.

Source: (StackOverflow)

.prop() vs .attr()

So jQuery 1.6 has the new function prop().

    //instead of:
    //do i use:

or in this case do they do the same thing?

And if I do have to switch to using prop(), all the old attr() calls will break if i switch to 1.6?


See this fiddle:

The console logs the getAttribute as a string, and the attr as a string, but the prop as a CSSStyleDeclaration, Why? And how does that affect my coding in the future?

Source: (StackOverflow)

jQuery scroll to element

I have this input element:

<input type="text" class="textfield" value="" id="subject" name="subject">

Then I have some other elements, like other text inputs, textareas, etc.

When the user clicks on that input with #subject, the page should scroll to the last element of the page with a nice animation. It should be a scroll to bottom and not to top.

The last item of the page is a submit button with #submit:

<input type="submit" class="submit" id="submit" name="submit" value="Ok, Done.">

The animation should not be too fast and should be fluid.

I am running the latest jQuery version. I prefer to not install any plugin but to use the default jQuery features to achieve this.

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)

How do I insert an Item into an array at a specific Index?

I am looking for a Javascript array insert method, in the style of:

arr.insert(index, item)

Preferably in jQuery, but any Javascript implementation will do at this point.

Source: (StackOverflow)

jQuery get specific option tag text

All right, say I have this:

<select id='list'>
    <option value='1'>Option A</option>
    <option value='2'>Option B</option>
    <option value='3'>Option C</option>

What would the selector look like if I wanted to get "Option B" when I have the value '2'?

Please note that this is not asking how to get the selected text value, but just any one of them, whether selected or not, depending on the value attribute. I tried:


But it is not working.

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"); = "none";
this.OuterDiv = odv;

var t = d.createElement("table");
t.cellSpacing = 0;
t.className = "text";

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");
// And many more

But I'm not sure if this is any better.

Source: (StackOverflow)

Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail

What would be a good way to attempt to load the hosted jQuery at Google (or other Google hosted libs), but load my copy of jQuery if the Google attempt fails?

I'm not saying Google is flaky. There are cases where the Google copy is blocked (apparently in Iran, for instance).

Would I set up a timer and check for the jQuery object?

What would be the danger of both copies coming through?

Not really looking for answers like "just use the Google one" or "just use your own." I understand those arguments. I also understand that the user is likely to have the Google version cached. I'm thinking about fallbacks for the cloud in general.

Edit: This part added...

Since Google suggests using google.load to load the ajax libraries, and it performs a callback when done, I'm wondering if that's the key to serializing this problem.

I know it sounds a bit crazy. I'm just trying to figure out if it can be done in a reliable way or not.

Update: jQuery now hosted on Microsoft's CDN.

Source: (StackOverflow)

Creating a div element in jQuery

How do I create a div element in jQuery?

Source: (StackOverflow)

Get current URL in JavaScript?

I am using jQuery. How do I get the path of the current URL and assign it to a variable?

Example URL:


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)

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 can I refresh a page with jQuery?

How can I refresh a page with jQuery?

Source: (StackOverflow)

How can I upload files asynchronously?

I would like to upload a file asynchronously with jQuery. This is my HTML:

<input type="file" id="file" name="file" size="10"/>
<input id="uploadbutton" type="button" value="Upload"/>

And here my JavaScript code:

$(document).ready(function () {
    $("#uploadbutton").click(function () {
        var filename = $("#file").val();

            type: "POST",
            url: "",
            enctype: 'multipart/form-data',
            data: {
                file: filename
            success: function () {
                alert("Data Uploaded: ");

Instead of the file being uploaded, I am only getting the filename. What can I do to fix this problem?

Current Solution

I am using the jQuery Form Plugin to upload files.

Source: (StackOverflow)