How can I trigger onblur from a ul li?
Asked Answered
W

2

6

I am essentially highlighting each li when clicked, and want to "un-highlight" the clicked li when clicking elsewhere or tabbing out (by effecting the background-color property).

This behavior would essentially be emulating <select> in its' highlighting behavior... I'm not using select though, because I want to nest HTML inside the listed items  --  you can't do this with <option>.

I'm attempting to use onblur, which is not working...

Here is the HTML:

<ul id = "list">
    <li>asdf</li>
    <li>qwerty</li>
<ul>

...here is the CSS:
    #list {
        list-style-type: none;
    }

...and here is the jQuery/Javascript:

    function getEventTarget(e) {
        e = e || window.event;
        return e.target || e.srcElement; 
    }

    function highlightSelection(selection) {
        alert("It worked!");
        $(selection).css("background-color","#FFFF00");
    }

    // this function is not being triggered:
    function removeHighlight(selection) {
        $(selection).css("background-color","none");
    }

    var ul = document.getElementById("list");

    ul.onclick = function(event) {
        var target = getEventTarget(event);
        highlightSelection(target);
    };

    // this event is not working:
    ul.onblur = function(event) {
        var target = getEventTarget(event);
        removeHighlight(target);
    };
Walters answered 15/7, 2013 at 20:31 Comment(0)
A
2

Since you're already using JQuery...

<ul id = "list">
     <li tabindex="1">asdf</li>
     <li tabindex="2">qwerty</li>
 <ul>


var ul = $("#list");
ul.on('click focus','li',function(){
    $(this)
        .css("background-color","#FFFF00")
        .siblings().css('background','');
}).on('blur','li',function(){
    $(this).css('background','');
});

http://jsfiddle.net/mc4tN/2/

I wasn't sure of what effect you wanted when you tab away from a list item... It seems you would just want to leave it highlighted. You can add focus-ability to your list items by giving them a tabindex attribute.

Aerology answered 15/7, 2013 at 20:44 Comment(2)
That's awesome, thanks @Bluetoft! How might I do this using addClass and removeClass instead of css?Walters
Ok nevermind, I got it -- $(this).addClass("highlighted").sibling().removeClass("highlighted"); in the contents of ul.on('click focus'... instead, where "highlighted" is a CSS class.Walters
H
3

The lis don't blur because they don't focus. Try with mouseout or mouseleave.

Huggins answered 15/7, 2013 at 20:40 Comment(6)
Thanks @Dek Dekku for the quick reply, is it possible to set the focus on the clicked li?Walters
Now that I'm looking better, you're actually setting the listener on the ul.Huggins
Okay, so I almost got it -- adding the attribute tabindex = "1" to ul, or whatever integer assignment, will give li's focus. However, this trick does not allow only one li to be highlighted at a time.Walters
call the removeHighlight method from highlightSelection to remove highlight on the previoously selected item, them set it on the new target.Huggins
Thanks again @Dek Dekku, do you mean to access the previously assigned target variable from the removeHighlight method?Walters
Yes, you can save it (I guess a global variable would do for now, but protecting the scope with a closure would be better) and call the method on it. Otherwise, rewrite the remove method to remove the class from all the "li"s rather than just the target: that's less efficient, but more bulletproof.Huggins
A
2

Since you're already using JQuery...

<ul id = "list">
     <li tabindex="1">asdf</li>
     <li tabindex="2">qwerty</li>
 <ul>


var ul = $("#list");
ul.on('click focus','li',function(){
    $(this)
        .css("background-color","#FFFF00")
        .siblings().css('background','');
}).on('blur','li',function(){
    $(this).css('background','');
});

http://jsfiddle.net/mc4tN/2/

I wasn't sure of what effect you wanted when you tab away from a list item... It seems you would just want to leave it highlighted. You can add focus-ability to your list items by giving them a tabindex attribute.

Aerology answered 15/7, 2013 at 20:44 Comment(2)
That's awesome, thanks @Bluetoft! How might I do this using addClass and removeClass instead of css?Walters
Ok nevermind, I got it -- $(this).addClass("highlighted").sibling().removeClass("highlighted"); in the contents of ul.on('click focus'... instead, where "highlighted" is a CSS class.Walters

© 2022 - 2024 — McMap. All rights reserved.