Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger onblur from a ul li?

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);
    };
like image 896
Ian Campbell Avatar asked Oct 03 '22 13:10

Ian Campbell


1 Answers

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

like image 139
Dek Dekku Avatar answered Oct 07 '22 18:10

Dek Dekku