Rails beginner
Rails beginner

Reputation: 14504

jQuery UL list how to hide div and hide dropdown

HTML:

<div id="colourlist">test 1</div>
<ul id="colours">
<li value="1">test 1</li>
<li value="2">test 2</li>   
<li value="3">test 3</li>   
<li value="4">test 4</li>       
</ul>
<div id="preview"></div>

jQuery:

$("#colours li").on('mouseenter', function(){
    $("#colours li").removeClass("hilight");
    $(this).addClass("hilight");
    var O = $(this).offset();
    var CO = $("#colours").offset();
    $("#preview").css("top", O.top-150).show();
    $("#preview").css("left", CO.left+160);
}).on('click', function(){
    var text = $(this).text();
    $("#colourlist").text(text);
    $("#colours").hide();
    $("#preview").hide(); 
});
$("#colourlist").on('click', function(e){
    e.preventDefault();
    $("#colours").toggle();   
});

Here is the jsfiddle: http://jsfiddle.net/CJbeF/48/

How do I hide the dropdown and preview when clicking outside the UL dropdown list?

How do I hide preview div when the dropdown is open and clicking on the dropdown again?

Upvotes: 1

Views: 199

Answers (2)

Th0rndike
Th0rndike

Reputation: 3436

You can add this event handler:

$("#colours").on('mouseleave', function(){
    $("#colours").hide();
    $("#preview").hide(); 
});

here's a fiddle: fiddle

Upvotes: 1

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

Why not to try multiple events in .on().

$("#colours li").on({
    mouseenter: function() {
        // Handle mouseenter...
    },
    click: function() {
        // Handle click...
    }
});

Upvotes: 0

Related Questions