JavaScript How to Close Twitter Bootstrap 3 Collapse Navbar upon Clicking a Menu Item twitter bootstrap close modal

I know this has been discussed in other threads, but I've very new to coding websites in general, and when I try to use the solutions others have come up with, they just don't seem to work. And I apologize if my terminology isn't quite right, but hopefully you'll be able to understand!

On the site I'm building on my localhost, I have it so that the Bootstrap navbar I'm using remains collapsed on desktop, tablet, and mobile. The navbar is a sticky nav, and when you select an item from the menu it scrolls down to the item's anchor. The problem is, since the menu remains open when an item is clicked, the dropdown covers part of the div it anchors to.

This is the code for my navbar:

<nav class="navbar navbar-default" role="navigation">
                  <div class="container-fluid">

                       <div class="navbar-brand">
                           <h2>My Title</h2>
                       </div>

                    <div class="navbar-header">
                      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                      </button>

                    </div>


                        <?php
                            wp_nav_menu( array(
                                'menu'              => 'menu-1',
                                'theme_location'    => 'primary',
                                'depth'             => 2,
                                'container'         => 'div',
                                'container_class'   => 'collapse navbar-collapse',
                                'container_id'      => 'bs-example-navbar-collapse-1',
                                'menu_class'        => 'nav navbar-nav',
                                'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
                                'walker'            => new wp_bootstrap_navwalker())
                            );
                        ?>
                    </div>
</nav>

I've seen solutions like this:

 $('.nav-collapse').click('li', function() {
 $('.nav-collapse').collapse('hide');
 });

But when I try adding it to my code it doesn't work. I've been putting it in my head, but I'm not sure if that's where I should be inserting it. Should I be putting that in the bootstrap.js file, or should I be downloading another javascript library?

Try to be very descriptive with your answer if you can, because to reiterate, I'm very new to this and I'm still in the process of learning.

Thanks so much,
Angela

Answer:1

you should add on the very bottom of your page a javascript part (right before ) Frm the code you paste, I assume that you have already loaded jQuery library too ($. is equivalent to jQuery.)

The code bellow adds an event on the ID element bs-example-navbar-collapse-1 with jQuery

 <script src="http://code.jquery.com/jquery-1.11.1.min.js type="text/javascript"></script>
 <script>

     $("#bs-example-navbar-collapse-1").click('li', function() {
        var element = document.getElementById("#bs-example-navbar-collapse-1");
        if (element.className == ".navbar-toggle"){
              $(".navbar-toggle").collapse('hide');
        }else
          console.log("Navbar has to be opened");
     });

    </script>
</body>
Answer:2

I want users to be able to take a picture programmatically of a design they have made on my site . The design would be in a div that is a panorama that extends off both left/right edge of the screen. ...

I want users to be able to take a picture programmatically of a design they have made on my site . The design would be in a div that is a panorama that extends off both left/right edge of the screen. ...

  1. screenshot is possible with htmlunitdriver
  2. screenshot if possible
  3. screenshot not possible

The new JS enabled page of http://mail.google.com is making trouble to get ID of password input field. However, I've navigated from email ID page with its ID - identifierID . Help me to get ID of ...

The new JS enabled page of http://mail.google.com is making trouble to get ID of password input field. However, I've navigated from email ID page with its ID - identifierID . Help me to get ID of ...

  1. password input field html
  2. password input field in react native
  3. password input field unity
  4. password input field in angular material
  5. password input field
  6. password input field css
  7. password input field django
  8. password input field validation
  9. password input field type html
  10. password input field type
  11. show password input field bootstrap
  12. show password input field
  13. angular password input field
  14. qt password input field
  15. show password input field jquery
  16. php password input field
  17. bootstrap password input field
  18. react password input field
  19. yii2 password input field
  20. hide password input field

The window.screenX and window.screenY properties only give the top left corner of the outer window, outside the content area (above the address bar and tabs). How can I determine the position of the ...

The window.screenX and window.screenY properties only give the top left corner of the outer window, outside the content area (above the address bar and tabs). How can I determine the position of the ...

I am trying to log into a website with Scrapy, but the response received is an HTML document containing only inline JavaScript. The JS redirects to the page I want to scrape data from. But Scrapy does ...

I am trying to log into a website with Scrapy, but the response received is an HTML document containing only inline JavaScript. The JS redirects to the page I want to scrape data from. But Scrapy does ...

  1. execute inline javascript
  2. execute javascript inline html
  3. node execute inline js
  4. javascript execute inline function