Dbz
Dbz

Reputation: 2761

Select only one selectable with jQueryUI

I have a serialized selectable, and I want a function to make it only select one list item at a time. I believe I have the right code, but it's not working. Anyone know why?

Here is my javascript:

$(function() {
    $( "#selectable" ).selectable({
        stop: function() {
            var result = $( "#select-result" ).empty();
            $( ".ui-selected", this ).each(function() {
                var index = $( "#selectable li" ).index( this );
                result.append( " #" + ( index + 1 ) );
            });
        }
    });
});


$("#selectable").selectable({
      selected: function(event, ui) {
            $(ui.selected).siblings().removeClass("ui-selected");
      }
});

My selectable from the html is below. Perhaps there is a problem due to the elements nested in the <li> tags?

<ul id="selectable">
    <li><a><img src="images/CacheMenuGoogleCache2.png", alt="Google Cache" class="button" id="Google Cache" height ="34px" /></a></li>
    <li><a><img src="images/CacheMenuBingCache2.png", alt="Bing Cache" class="button" id="Bing Cache" height ="34px" /></a></li>
    <li><a><img src="images/CacheMenuYahooCache2.png", alt="Yahoo Cache" class="button" id="Yahoo Cache" height ="34px" /></a></li>
    <li><a><img src="images/CacheMenuWaybackMachine2.png", alt="WayBack Machine" class="button" id="WayBack Machine" height ="34px" /></a></li>
    <li><a><img src="images/CacheMenuCoralCDN2.png", alt="CoralCDN" class="button" id="CoralCDN" height ="34px" /></a></li>
    <li><a><img src="images/CacheMenuGigablast2.png", alt="Gigablast" class="button" id="Gigablast" height ="34px" /></a></li>
    <li><a><img src="images/CacheMenuWebCite2.png", alt="Webcite" class="button" id="Webcite" height ="34px" /></a></li>
</ul>

I appreciate your time and your help!

Upvotes: 0

Views: 2271

Answers (1)

Dbz
Dbz

Reputation: 2761

Okay, I found the problem. I needed to include it inside of a function like so:

$(function() {
    $("#selectable").selectable({
        selecting: function(event, ui){
            if( $(".ui-selected, .ui-selecting").length > 1){
                  $(ui.selecting).removeClass("ui-selecting");
            }
        }
    });
});

Also, you might notice the function is a little bit different. That is because the other function left the images selected.

Upvotes: 3

Related Questions