Inspect length, append a step, or replace the queue — always finish custom steps.
Get a raw DOM node ($el[0] or this in a callback).
Read with $.queue(el, "fx") and check .length.
Append with $.queue(el, "fx", fn) — end fn with $.dequeue(this).
Clear pending steps with $.queue(el, "fx", []) (often plus .stop()).
Piece
Required?
Role
element
Yes
Raw DOM element that owns the queue
queueName
Usual
Defaults to "fx" when reading
callback / newQueue
Optional
Append one function, or replace with an array
Pseudocode
Pseudocode
function queue(element, name = "fx", third):
if third is missing:
return array of pending functions
if third is a function:
append third to element.queues[name]
return the queue array
if third is an array:
replace element.queues[name] with third
return the queue array
// After appending a custom step:
callback must eventually call dequeue(element, name)
Reference
Cheat Sheet
Keep these facts handy while working with jQuery queues.
Queue length is: 3
(exact number depends on timing)
How It Works
Enqueue. Each fade call adds a step to the fx queue.
Inspect.jQuery.queue(el, "fx") returns the pending array.
Length.n.length is how many steps are still waiting or in flight.
Timing. Check again later and the number may drop as steps finish.
Example 2 — Official: Append a Custom Function
Add a mid-step with $.queue, then continue with $.dequeue.
jQuery
var el = $("#box")[0];
$("#box").fadeOut(400);
jQuery.queue(el, "fx", function () {
$(this).text("Mid step");
jQuery.dequeue(this);
});
$("#box").fadeIn(400);
Append.$.queue(el, "fx", fn) adds your custom work.
Work. Inside fn, update the text (or class).
Continue.$.dequeue(this) unlocks fadeIn.
Example 3 — Official: Replace Queue with []
Drop pending steps by replacing the queue with an empty array.
jQuery
var el = $("#box")[0];
$("#box").fadeOut(2000).fadeIn(2000).fadeOut(2000);
// Later — cancel waiting steps:
jQuery.queue(el, "fx", []);
$("#box").stop();
One line:$.queue reads or rewrites an element’s function list — append carefully, and always dequeue.
Frequently Asked Questions
jQuery.queue() shows or changes the function queue on a DOM element. With only an element (and optional queueName) it returns an Array of pending functions. With a callback it appends that function. With an array it replaces the whole queue. Added in jQuery 1.3. Prefer $(element).queue() for everyday code.
1) jQuery.queue(element [, queueName]) — returns Array (inspect length). 2) jQuery.queue(element, queueName, callback) — adds a function to the end. 3) jQuery.queue(element, queueName, newQueue) — replaces the queue with that Array (use [] to clear pending steps).
Same jobs, different call shape. jQuery.queue(elem, …) takes a raw DOM element first. .queue() is called on a jQuery collection. Official docs recommend .queue() day to day; jQuery.queue() is the low-level utility.
"fx" is jQuery’s default effects queue. .animate(), .fadeOut(), and similar methods push steps onto fx and run them one at a time. Omitting queueName when reading defaults to fx.
A function you append with jQuery.queue(el, "fx", fn) pauses the pipeline until you finish. Call jQuery.dequeue(this) (or $(this).dequeue() / next()) so the next step runs.
It replaces the fx queue with an empty array, dropping pending functions that have not started. Pair with .stop() when canceling animations. .clearQueue() is the dedicated instance API for clearing pending items.