I am calling a method getSelectedIndex
from a plugin Megalist, like this:
var selectedIndex = $('#left-list').megalist('getSelectedIndex');
However, instead of returning an integer, I get a jQuery object representing the list instead. I tried to call the method directly, both as
var selectedIndex = $('#left-list').getSelectedIndex();
and
var selectedIndex = $('#left-list').megalist().getSelectedIndex();
but then I get an error stating there is no method named getSelectedIndex
. Inserting .eq(0)
to isolate only the first jQuery object also didn't work. I tried googling for this, but these few pages don't seem to provide an answer. The method is defined as follows:
getSelectedIndex: function() {
return parseInt(this.selectedIndex, 10);
},
and should return an integer. How can I just call the method to return an integer instead?
Update: It seems that doing something like this:
window.selectedIndex = -1;
// ...
function listChangeHandler( event ) {
// ...
window.selectedIndex = event.selectedIndex;
// ...
}
// ...
$('.favorite-link').on('click', function(){
if (window.selectedIndex != -1)
{
if (window.favorites.indexOf(window.selectedIndex) == -1)
{
window.favorites.push(window.selectedIndex);
}
}
});
does the trick, however I would still want to know if this can be done without using global variables.