I'm using bootstrap 3.3.6 and jQuery 1.11.3. I keep struggling with triggering bootstrap's navbar using plain JavaScript/jQuery.
My application is giving me the following data structure. Unfortunately, I don't have the ability to change it. I can add neither classes nor ids nor anything else.
<ul>
<li><a href="http://google.de">Stuff</a>
<ul>
<li><a href="#">Action</a></li>
<li><a href="#">Something else here</a></li>
<li><a href="#">One more link</a></li>
</ul></li>
<li><a href="#">Mgmt Reports</a></li>
<li><a href="#">Notices</a></li>
<li><a href="#">Liens</a>
<ul>
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a>
<ul>
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">A long sub menu</a>
<ul>
<li><a href="#">Menu item 1</a></li>
<li><a href="#">Menu item 2</a></li>
<li><a href="#">Menu item 3</a></li>
</ul>
</li>
<li><a href="#">Another link</a></li>
<li><a href="#">One more link</a></li>
</ul>
</li>
</ul>
</li>
</ul>
What I have already done in order to add the bootstrap navbar:
Adding the class
dropdown-menuto allultags. I used$("ul ul").addClass("dropdown-menu");to achieve that.Adding the classes
nav navbar-navto the outermostultag. I used$("ul:first").addClass("nav navbar-nav");to achieve that.Adding a surrounding
divwith classescollapse navbar-collapse. This is not a problem as I'm able to edit the surrounding HTML.Adding jQuery Smartmenus to get multilevel navbar working.
The classes are added nicely (the outcoming code looks good). This is the working jQuery code:
$("ul:first").addClass("nav navbar-nav");
$("ul ul").addClass("dropdown-menu");
$("ul:first").smartmenus({
subIndicatorsText: '',
subIndicatorsPos: 'append',
});
$(".sub-arrow").addClass("caret");
EDIT
The navbar is triggered. However, it is not equal to a navbar on which the same css classes are added manually (in HTML without JavaScript). It doesn't have the same functionality.
Example 1: A normal bootstrap navbar prevents the TopMenu items from being clicked on a mobile device. I.e. the 1st click on a mobile device on <a href="http://google.com">Stuff</a> opens the SubMenu. The 2nd click opens google. This doesn't seem to work when the classes are added using JavaScript.
Example 2: When I add the classes manually the submenus open to the bottom (dropdown). When the classes are added using jQuery the submenus open in right direction. The outcoming code looks the same.
Question: What am I missing here? How can I trigger the bootstrap navbar using plain JavaScript or jQuery without touching the HTML and without losing functionality?
lielement. This may be causing you problem. It is the third line up from the bottom in your example.lielement is closed before theulthat should be its child. Thisulis then a child of anotherul, which is invalid, and the extra</li>should be causing you an error. Remove the closinglitag (</li) from line 3 in your example.