Home >>jQuery Tutorial >jQuery $.proxy() Method
jQuery $.proxy method in jQuery is used to add events to an element where the context is pointing back to a different object and returns a new one with a particular context.
Syntax:$(selector).proxy(function,context) $(selector).proxy(context,name)
Parameter | Description |
---|---|
function | To call the existing function |
context | Where the function lies to the name of the object |
Name | Context will be of the existing function |
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var objPerson = {
name: "Alexa",
age: 38,
test: function(){
$(".txt").after("Name: " + this.name + "<br> Age: " + this.age);
}
};
$(".btn1").click($.proxy(objPerson, "test"));
});
</script>
</head>
<body>
<button class="btn1">click to run test function</button>
<p class="txt"></p>
</body>
</html>