tags:

views:

480

answers:

3

Using the jQuery UI library selectable(). Links within the selectable list items are not being followed on click, only by right clicking and opening in new window or tab.

HTML

<ul class="selectable-list">
    <li>
        <p>Visit Google.</p>
        <a href="http://www.google.com"&gt;Google&lt;/a&gt;
    </li>
    <li>
        <p>Visit Apple.</p>
        <a href="http://www.apple.com"&gt;Apple&lt;/a&gt;
    </li>
    <li>
        <p>Visit Microsoft.</p>
        <a href="http://www.microsoft.com"&gt;Microsoft&lt;/a&gt;
    </li>
</ul>

CSS

.selectable-list li.ui-selected, .selectable-list li.ui-selected:hover {
    background-color: #ccc;
}

JS

$(document).ready(function(){

    $(".selectable-list").selectable();

});
+1  A: 

Selectable is overriding your click events on the href. You're probably going to have to add back in the navigate functionality.

However, that seems like odd behavior you are shooting for. Why would I ever want to both select something and follow a link (presumably to a new window, which would then take focus) at the same time?

Russell Steen
Yes, I see that selectable is overriding the normal click functionality of the link. The question then is what are some suggestions on the best way to fix it?The user will select items in a list to save to a personalized list. The selectable() interaction shows which items have been selected so far. They may also choose to follow the link in the list item.
jerome
Well, having it automatically follow the link is probably not what you want from a UI standpoint. If it is, then use Jquery more to expand the OnClick functionality after you call .selectable().However, I'd recommend having either the link be active with (select) afterwards, or vice versa, where the click is a select, but you add a (open url) link on the same line with an on click to open the URL.
Russell Steen
I appreciate your recommendations on the UI behavior. The actual code that I am working on consists of a list item wrapping a link to an article and a longer description of an article entry, followed by a date stamp for that article. So the content within the li is bigger, and it is the li that is selectable, not the link itself. Perhaps that description will help you imagine what I am trying to do. Do you have a specific recommendation on how to leave the li as a whole selectable, while retaining (or re-adding) the link functionality?
jerome
Yes, it does make a lot more sense now. I see where you are going with it. I do not have a specific recommendation. Start with overriding the onlick for just that element, I recommend by adding and onlick('hi') to the sub elements, after you call selectable(). That's where I'd start. I do not know specifically how sub-element click events are handled in priority with parent elemetn click events.
Russell Steen
+1  A: 

The selectable() method takes an option called cancel. By default, it is ":input,option", but you can use it to make your anchors not applicable to the selectable event.

For your code:

$(document).ready(function() {
    $(".selectable-list").selectable({
        cancel: 'a'
    });
});
Lance McNearney
A: 

The tip about the 'cancel: a' exactly did the trick for me.

I guess this functionality is not too strange. I am working on a photo application and I have clickable thumbnails that take you to a larger version, but there are also selectable 'handles' that let you create a selection of images to take some action on (tag, delete, move, etc).

That being said, I am tempted to request or work on adding some more features to the plugin. I would expect ctrl-click on a selected element to unselect.

Again, thanks for the tip!

Babbagej