How can you pass a string via proxy when binding event handlers? I want to pass a data attribute that is attached to the target handler to a method of an object. Is this possible?
function ReservationSchedulePicker(reservationType){
//Reservation Type Accepted Use: 'routine' || 'vacation'
this.reservationType = reservationType;
//DIV for Schedule Picker
this.schedulePickerDiv = $("#schedulePicker");
//Add Event Handler to Anchor
$(this.schedulePickerDiv).on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this));
}
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function(event, day) {
//Trying to pass the data-day value to this function using proxy
}
//I need the data-day value inside of openAddWalkDialog Is this possible?
<a href="#" id="addWalk" data-day="monday">
How can you pass a string via proxy when binding event handlers? I want to pass a data attribute that is attached to the target handler to a method of an object. Is this possible?
function ReservationSchedulePicker(reservationType){
//Reservation Type Accepted Use: 'routine' || 'vacation'
this.reservationType = reservationType;
//DIV for Schedule Picker
this.schedulePickerDiv = $("#schedulePicker");
//Add Event Handler to Anchor
$(this.schedulePickerDiv).on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this));
}
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function(event, day) {
//Trying to pass the data-day value to this function using proxy
}
//I need the data-day value inside of openAddWalkDialog Is this possible?
<a href="#" id="addWalk" data-day="monday">
Share
Improve this question
edited Jul 24, 2020 at 20:41
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Jan 3, 2014 at 21:58
Justin HarrisJustin Harris
2892 gold badges3 silver badges12 bronze badges
0
3 Answers
Reset to default 2Is this what you are looking for:
<script type="text/javascript">
function ReservationSchedulePicker(reservationType){
this.reservationType = reservationType;
this.schedulePickerDiv = $("#schedulePicker");
this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this));
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function(event) {
event.preventDefault();
alert(this.reservationType);
}
$(document).on('ready',function(){
var x = new ReservationSchedulePicker('hello world');
});
</script>
<div id="schedulePicker">
<a href="#" id="addWalk" data-day="monday">Hello</a>
</div>
Update 1: Based additional details provided in ments
<script type="text/javascript">
function ReservationSchedulePicker(reservationType){
this.reservationType = reservationType;
this.schedulePickerDiv = $("#schedulePicker");
this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this, $("#addWalk").attr('data-day') ));
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function(attr, event) {
event.preventDefault();
alert(this.reservationType + '=>'+ attr);
}
$(document).on('ready',function(){
var x = new ReservationSchedulePicker('hello world');
});
</script>
<div id="schedulePicker">
<a href="#" id="addWalk" data-day="monday">Hello</a>
</div>
Update 2
<script type="text/javascript">
function ReservationSchedulePicker(reservationType){
this.reservationType = reservationType;
this.schedulePickerDiv = $("#schedulePicker");
this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this ));
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function( event) {
event.preventDefault();
alert(this.reservationType + '=>'+ ($(event.target).data('day'))) ;
}
$(document).on('ready',function(){
var x = new ReservationSchedulePicker('hello world');
});
</script>
<div id="schedulePicker">
<a href="#" id="addWalk" data-day="monday">Hello</a>
</div>
What about something like reference arguments to proxy:
$(this.schedulePickerDiv).on('click', '#addWalk', $.proxy(this.openAddWalkDialog, this, { foo: bar }));
Or on the event.data object (event.data.foo):
$(this.schedulePickerDiv).on('click', '#addWalk', { foo: bar }, $.proxy(this.openAddWalkDialog, this));
This should do the trick:
var that = this;
$(this.schedulePickerDiv).on("click", "#addWalk", function(e){
that.openAddWalkDialog.call(this, e);
});
ReservationSchedulePicker.prototype.openAddWalkDialog = function(event) {
console.log(event, $(this).data('day'));
}