How to collapse a bootstrap navbar programmatically

Multi tool use


How to collapse a bootstrap navbar programmatically
I'm trying to build a website using bootstrap which contains a navbar. The mobile site looks pretty good until you try to change the websites content using the navbar. If you click it, the content changes, but the navbar stays expanded. When you try closing it manually, the content disappears and the initial content shows up.
HTML:
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header" id="mainpage">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
...
<li id="examplenav"><a href="#">Example</a></li>
JS:
$("examplenav").addEventListener("click", showExampleContent);
showExampleContent is a function that changes a div using dom scripting.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.