English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery $.proxy() method

jQuery Events

$.proxy() method accepts an existing function and returns a new function with a specific context. This method is usually used to add events to elements pointing to different objects in the context.

Syntax1:

jQuery.proxy(function, context)

Syntax2:

jQuery.proxy(context, name)

Example

To enforce the context of the "getFullName" function within myObj:

let myObj = {
  fname: "Seagull",
  lname: "Anna",
  age: 22,
  getFullName: function(){
$("p").after("First Name: " + this.fname + "<br>Last Name: " + this.lname);
  }
};
$("button").click($.proxy(myObj, "getFullName"));
Test to see‹/›

Parameter Value

ParameterDescription
functionThe function whose context will change
contextThe object to which the function's context (this) should be set
nameRename the function that changes its context (should be the property of the context object)

jQuery Events