1

I am trying generating a dynamic bootstrap 3 dropdown menu for sharing on mouse hover.

See this picture

Dropdown menu

Instead of creating lots of HTML code I decided to generate dynamic menu. Here is what I have done so far:

HTML

<div class="dropdown dropup" >
    <a href="#" class="open-dropdown" data-link="http://google.com">Dropdown</a>
    <ul class="dropdown-menu" role="menu">
        <li id="link1">Link One</li>
        <li id="link2">Link Two</li>
        <li class="caret"></li>
    </ul>
</div>

JQuery

$(function() {
    $('.open-dropdown').hover(function(){
        $('#link1').html("Share link 1 "+$(this).data('link'));
        $('#link2').html("Share link 2 "+$(this).data('link'));
        $('.open-dropdown').dropdown();
    });
});

Problems

  1. It opens the dropdown menu on click not on MOUSE HOVER.
  2. I can't place <ul class="dropdown-menu" role="menu"> code outside of <div class="dropdown dropup"> container. See this jsfiddle http://jsfiddle.net/LTk3x/

2 Answers 2

1

What about just using some CSS ? :)

.dropdown:hover .dropdown-menu{
    display: block;    
}

Here is a jsFiddle:
http://jsfiddle.net/hMp2r/

Sign up to request clarification or add additional context in comments.

6 Comments

@Nifhel why caret position is misplaced? Also did you check my second problem?
sorry but I really don't get the second problem, could you provide a jsfiddle with an example please? And caret was like that when I included the bootstrap elements, it is the same in the other answer. But it might be working correctly for you if you didn't have this issue before.
Nope this is the one I created... And I don't see the relation with the second point, or please be more explicit.
I want to use <ul class="dropdown-menu" role="menu"> outside of the <div class="dropdown dropup" > because I want to reuse the dropdown code on unlimited posts. I hope it makes sense now.
But I don't get why you would not be able to reuse it if it's not outside of the dropdown div... So an example would be nice. If you say it is not working you should have some code to show us.
|
1

You can change $('.open-dropdown').dropdown(); by $('.dropdown-menu').toggle();

$(function() {
    $('.open-dropdown').hover(function(){
        $('#link1').html("Share link 1 "+$(this).data('link'));
        $('#link2').html("Share link 2 "+$(this).data('link'));
        $('.dropdown-menu').toggle();
    });
});

see http://jsfiddle.net/32xLc/1/

7 Comments

Thanks @inye have you checked my second problem?
why you need put the ul elemento outside of div container ?
I want to reuse this code on hundreds of posts on a page.
yes, this dropdown will appear on all sharing links
I dont understan very well what you want. Maybe you can create a fiddle.jshell.net example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.