0

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.

0

2 Answers 2

1

When the plugin is initialised, no items are selected. Just bind a change handler to #left-list, you can get the selectedIndex from the event object:

$('#left-list').on('change', function(e) {
  var selectedIndex = e.selectedIndex;
});

Check the documentation

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, but I was doing something like this: $('.favorite-link').on('click', function(){ selectedIndex = $('#left-list').megalist('getSelectedIndex'); if (window.favorites.indexOf(selectedIndex) != -1) { window.favorites.push(selectedIndex); } }); Should I resort to using a global variable then?
I've updated the question with a workaround. I would still like to know if this could be done without using the global variables.
1

When an item in the megalist has been selected, the class .megalistSelected is added to the list item so you could search for a <li> with that class using jQuery and get the value of its list-index attribute.

With the following megalist markup as an example:

   <div class="megalist" id="myList2">
         <ul style="visibility: visible; left: 0px; top: 0px;">
            <li class="megalistItem megalistSelected" list-index="1" style="left: 0px; top: 41px;">Decimal: 1, Hex: 1</li>
         </ul>
   </div>

Then...

$('#myList2').find('.megalistSelected').attr('list-index')

...will retrieve the index of the item (once an item has been selected of course).

1 Comment

Yes, that could work, but it still relies on implementation rather than the plugin interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.