Code has been added to clipboard!

Using jQuery .proxy() Method Explained

Reading time 1 min
Published Jan 10, 2018
Updated Oct 10, 2019

jQuery proxy: Main Tips

  • The jQuery .proxy() method takes an existing function and returns a new one with a specific context.
  • Since jQuery 1.6, .proxy() accepts unlimited number of additional arguments. They will be passed to the function whose context is set to change.

Using the .proxy() Method

When JavaScript executes code, every called function has its execution context. An execution context consist of the activation object, scope chain and this value.

There are two methods for changing the value of this: .call() and .apply().

jQuery .proxy() serves the same purpose: it changes the value of this for a specific function and returns that new function.

The example below sets the context of the info function inside personObject:

Example
$("button").click($.proxy(personObject, "info"));

Syntax for jQuery .proxy()

The syntax of jQuery .proxy() is as follows:

$("selector").proxy(name, context);

Let's review the arguments jQuery .proxy() takes:

  • name is the name of the function whose context will be changed.
  • context represents the object to which the context of the selected function will be changed.
  • You can also add extraArguments at the end.

Note: changing the order of parameters makes no difference – the .proxy() jQuery method would still work.

Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX