I am creating a jQuery widget. I have this code example set up to match my original situation as closely as possible...
function someFunction(){
$("#somediv").on("click", ".rem", $.proxy(this.handleEvent, this));
}
function handleEvent(that){
var testval = $(that).parent().index();
console.log(testval);
}
someFunction();
JSBin here ----> http://jsbin.com/cewevo/
The issue is that, regardless of the 'X' button clicked on, the index result is '-1'. I have tried every element path I can think of, but the best I can do is get the innerText of the related button click. I need the index so that I can delete the correct element in the original array that was used to populate the list.
Code like this works as expected, except the lines below does not work for me in the widget context.
$("somediv").on("click", ".rem", function(){
var testval = $(this).parent().index();
});
I am using the .proxy function to try to get the right context to the function "handleEvent".
I don't think that using
$.proxy
is the best option here since, in your casehandleEvent
always receives the mouse event as argument.The following code gives the result that you want.