Toggle the navigation bar

Visit our webpage at:

You can also check out the code on Codepen: https://codepen.io/mitchellbarron10/pen/EqZjQj

This project involves JavaScript embedded in html code.

The task at hand is to ensure that the navbar (located on the top right corner of the page) remains closed upon loading. When clicked, it should open, and when the X button on the navbar is clicked, it should close.

I attempted to use separate buttons for openbtn and closebtn functionalities.

<div id="main">
<button class="openbtn" onclick="openNav()">&#9776;</button> 
<ul id="menuicons">
  <a href="../html-link.htm"><img src="images/noun_Login_801390.png" width="82" height="86" title="White flower" alt="Flower"></a>
  <a href="../html-link.htm"><img src="images/noun_Shopping Cart_17861.png" width="82" height="86" title="White flower" alt="Flower"></a>
 </ul>
</div>
<script type="text/javascript">/* Set the width of the sidebar to 250px and the left margin of the page content to 250px */
function openNav() {
 document.getElementById("mySidebar").style.width = "250px";
 document.getElementById("main").style.marginLeft = "250px";
}

/* Set the width of the sidebar to 0 and the left margin of the page content to 0 */
 function closeNav() {
  document.getElementById("mySidebar").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
 }    </script>
 <script type="text/javascript">/* Set the width of the sidebar to 250px and the left margin of the page content to 250px */
 function openNav() {
 document.getElementById("mySidebar").style.width = "250px";
 document.getElementById("main").style.marginLeft = "250px";
}

/* Set the width of the sidebar to 0 and the left margin of the page content to 0 */
function closeNav() {
  document.getElementById("mySidebar").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
}</script>

Answer №1

To trigger a function when the page is first loaded, you can utilize the onload="myfunction()" event. For example, if you want to execute the closeNav() function at page load, you would use: <body onload="closeNav()".

With <body onload="closeNav()"> added to your current codepen, the right menu will not display upon initial page load. To then open the navbar by clicking on another button, simply add the onClick="openNav()" event to the desired element.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

My DIV elements are not adapting to the row and column breakpoints as expected. Can anyone help me understand why this is happening?

Currently, I am working on setting up a row of highlighted products on my website. It displays fine with 5 products on desktop view, but the challenge arises when it comes to viewing it on medium size devices where I want only 2 products to show, and event ...

Is it possible to set a single variable value for multiple attributes simultaneously in Javascript?

I would like to specify the horizontal padding of a styled element as variable 'x' and the vertical padding as variable 'y'. Here's one way to achieve this: var docex = document.getElementById.style; docex.paddingTop = y; docex.pa ...

How can I code a script to import JSON data into MongoDB database?

I have a JSON file named "data.json" that contains an array of people's names as shown below: "data": [ { { "name":"John", "age":30, } { "name":"Mark", "age":45, } } ] I am ...

The sidebar's bottom gets concealed beneath the page's window

I seem to be facing an issue with the sidebar where the bottom section is not visible. Interestingly, when I remove the top navbar, everything works fine. Despite my efforts to troubleshoot and apply various solutions, none of them seem to work for me. Do ...

Leveraging Flexbox and flexGrow in conjunction with Textfield

I am looking to use Flexbox to separate my components on the screen in a way that positions them correctly. I have 3 rows with textfields that need specific ratios related to the full width: row 1 : 2, 1, 1 row 2 : 1, 1 row 3 : 1, 1, 2 I attempted to ach ...

Utilizing jQuery and Ajax to efficiently load images

I am currently working on a project where an image tag triggers a jQuery function using AJAX to download images from another source to the server upon being clicked. However, I am facing an issue where when the image is clicked, it opens a new tab and atte ...

Establishing the initial value for an input file form

Similar Question: Dynamically set value of a file input I'm currently working with an HTML form that includes a file input field, and I'm attempting to set the initial value for the file path in the form. I've tried changing the "value" ...

Show the date and time in a visually appealing way by using HTML and JavaScript in an HTA application with scrolling effects

I have the following JavaScript code to show the current date in the format Mon Jun 2 17:54:28 UTC+0530 2014 within an HTA (HTML application). Now, I would like to display it as a welcoming message along with the current system date and time: Mon Jun 2 17: ...

What is the best way to implement this (click) function in Angular?

My goal is to create a set of three buttons with a (click) function attached to each. Initially, when the page loads, the first button should be selected while the other two are unselected. If I click on an already selected button, it should remain selecte ...

CSS not being properly rendered on newly inserted row within table via jQuery

I'm currently working on a table that allows users to select rows, and I've implemented some CSS to highlight the selected row. The issue arises when new rows are added to the table as they do not get highlighted when selected. HTML Portion: &l ...

Implement a click event for the newly added item

I am struggling to attach a click event to an element that I dynamically added using jQuery. When the item is present in my HTML code, everything works as expected. Please refer to the code below for clarification: HTML <div id="addNew">Add new Ite ...

What is the process for submitting a record to a table following the activation of a JavaScript link or button

I am working on creating a like-unlike button below each post for registered users to interact with. I have successfully created the button itself, but now I need to figure out how to store records when a user clicks the button. My idea is to utilize a tab ...

AngularJS $compile function is failing to render a custom template

Hi there, I am facing an issue while trying to render a custom template using the $compile function. The error message I keep getting is: unrecognized expression: {{senddata}} I have provided my code below for reference: app.controller('MainCtrl&ap ...

Disable the button on smaller devices

I'm trying to make my button take up the full width on smaller screens like mobile devices. Here's what I have so far... Large Device https://i.sstatic.net/TJex1.png Medium Device https://i.sstatic.net/Hvkan.png Small Device https://i.sstatic ...

A minuscule variation in width percentage due to a single pixel

I am experiencing an issue with a one pixel margin between two green colors on my website. I have been unable to locate the source of this problem. I am currently using display:inline-block. Here is the link to my website: ...

The integration of jQuery in PHP is experiencing some technical challenges

There's one issue driving me crazy: I created a left-floating file tree view and included it in my website. However, when I include my website in the file treeview, it becomes unclickable in Chrome, and in IE 9, the thumbnail part for viewing PDF docu ...

php Use cURL and the DOM to extract content with specified CSS styling

Unclear in the title, I want to copy all content from a specific div on an existing webpage (not owned by me). The code successfully extracts the content. Extractor code: // Get Data $curl_handle=curl_init(); curl_setopt($c ...

What is the proper method to deactivate a hyperlink in Angular 2?

I'm currently developing a web application using Angular2, and I find myself in need of displaying, but making it non-clickable, an <a> element within my HTML. Can anyone guide me on the correct approach to achieve this? Update: Please take no ...

Enhance your website's visual appeal with the Bootstrap-4 album card display feature that

I was inspired by the card concept found at https://getbootstrap.com/docs/4.0/examples/album/ to create a similar layout. However, I don't want to display all my cards at once as it could overwhelm viewers with 100 cards. Has anyone developed a JavaS ...

What is the best way to incorporate autoplay video within the viewport?

My objective is for the video to automatically start playing when it enters the viewport, even if the play button is not clicked. It should also pause automatically when it leaves the viewport, without the need to click the pause button. <script src=& ...