swapclass and initial padding

Firstly, I am not fluent in English. Secondly, my issue pertains to "toggleClass".

I am a newbie to jQuery and JavaScript, and here is my code snippet:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script> 
$(document).ready(function(){
   $('.humburger').click(function(){
       $('#sidbar').toggleClass('side-lg-tog');
       $('#contenu').toggleClass('contenu-lg-tog');
   });
});
</script>

<style> 
   body {
       margin:0; padding:0;
   }

   #entete {
       height:50px;
       background-color:#197aac;
       position:relative;
   }

   .humburger {
       position:absolute;
       top:17px;
       left:15px;
       cursor:pointer;
   }

   .humburger span {
       background-color:#fff;
       display:block;
       height:2px;
       width:20px;
       margin-bottom:3px;
   }

   #sidbar {
       width:300px;
       height:100%;
       background-color:#e7e7e7; 
       border-right:1px solid #ccc;
       position:absolute;
       top:50px;
       left:0;
       transition:transform .3s ease-in-out;  
   }

   .side-lg-tog{transform:translate(-240px, 0);}
   .contenu-lg-tog {margin-left:60px;!important}

   #contenu {
       background-color:#eff8fd;   
       height:100%;
       margin-left:300px;
       padding:15px 30px;
       transition:margin-left .3s ease-in-out;
   }

   .pad-contenu {
       border:1px solid #ccc;
       min-height:300px;
       background-color:#fff;
   }

   @media (max-width:767px){
    #sidbar{transform:translate(-300px, 0);color:red;}
    #contenu {margin-left:0;}
   }   
</style>
</head>
<body>
    <div id="entete">
       <div class="humburger">
          <span></span>
          <span></span>
          <span></span>
       </div>
    </div>
    <div id="sidbar">test</div>
    <div id="toggle-btn"></div>
    <div id="contenu">
        <div class="pad-contenu">
        </div>
    </div>

</body>
</html>

My problem arises when I click on the humburger icon. The sidebar transitions perfectly using the new class side-lg-tog

$('#sidbar').toggleClass('side-lg-tog');
, but the content remains with the initial margin-left:300px;. I would like it to be 60px. Thank you and apologies for any language errors, as French is my second language.

Answer №1

There was an error in the code snippet

.contenu-lg-tog {margin-left:60px;!important}
.

The correction requires adding a semicolon after "!important". The corrected code is:

.contenu-lg-tog {
   margin-left: 60px !important;
}

Answer №2

In my opinion, achieving this can be done through the following methods:

document.getElementsById("id")[0].setAttribute("class", "democlass");

Here, [0] denotes the element you wish to modify in sequential order.

Another way to accomplish this task is by using the syntax:

element.style.CSS_Property = "Value";

Where element represents the variable. For more information, please refer to: W3SCHOOL JS DOM

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

JQuery Scrollbar Feature Automatically Scrolls When Focused

Within my contact form, I have integrated reCaptcha and am utilizing the jQuery mCustomScrollbar plugin. The issue at hand is as follows: When I click on or focus on the reCaptcha field, the page automatically scrolls to the top of the div. To see a demo ...

Display page contents will appear when hovering over an image in Dropzone

I'm currently working on integrating Dropzone into an asp.net page. The drag-and-drop functionality for images is functioning properly, but I am encountering an issue where hovering over the image displays the contents of the entire page in the hover ...

Using jQuery to display additional fields within a list

My goal is to reveal new fields up to a maximum of 10 each time a button is clicked. At the moment, my code looks like this: $('.add_field').on('click', function(){ $(this).next('.field1').fadeIn(); return false; }); ...

Choose the most recently created tab using this function in jQuery UI Tabs

Can anyone assist me in modifying this function used to generate JQuery UI Tabs? I would like the last tab that is generated to be automatically set as the opened tab. Any help is greatly appreciated, as I am feeling quite confused at the moment. Thank y ...

Header formatting issue when attempting to implement tablesorter plugin

I implemented the widget-scroller and widget column Selector in my table using the table sorter plugin in jQuery. Has anyone encountered a problem like the one shown in this picture? It seems that my table headers do not align with the columns properly an ...

A guide to dynamically adding an HTML class using JavaScript

I have a login page with a text field for email and password, styled using the Bootstrap framework. I want to change the border color of the input field from grey to red when it is empty and the user clicks the login button. I tried implementing the code t ...

Is it possible to create this layout using Bootstrap 5? I want to stack spans, with one inside the container and one outside

Trying to achieve a specific design using Bootstrap 5.3. https://i.sstatic.net/STifm.jpg This design should be placed over the showcase, with the following code for the showcase displayed below: <div class="showcase d-flex flex-column"&g ...

Steps for implementing target='_blank' on a <Link> tag with more than just an <a> element inside

I'm facing an issue where I need to open a new browser tab and redirect to a specific URL when a button is clicked. The problem arises when using an element other than an anchor tag inside the Next's <Link> element, as it seems to ignore th ...

Creating interactive web elements using AJAX, jQuery, and Laravel for dynamically updating dropdown menus and transferring selected values to a text box

I am trying to display data in a textbox by selecting an option from a dropdown menu on my view. The dropdown menu is populated with data from a table called aircraft_registration_number. When a user selects an option from the dropdown, I want to fetch the ...

The CSS class in jQuery Mobile is not being properly displayed on the input field

I'm having trouble getting the .ui-input-text class to apply to the input field, despite using the jQuery Mobile class for text inputs. The border radius is not changing unless I manually add it in with CSS code. Here's my current setup: <!DO ...

"Initiate file upload using jQuery ajaxForm when dropping a file onto a designated div element

I am utilizing the jquery form plugin for Ajax file upload, and below is an example of how I have it set up: <form enctype="multipart/form-data" action="upload.php" id="form" method="post"> <input type="file" name="upfile" size="44" /> ...

Refresh the page when the dropdown selection is changed and send the new value

I'm currently working on a page that includes a dropdown menu. My goal is to have the page reload after selecting an option from the dropdown so that it returns to the same page with the selected value passed through. Here's the PHP code for th ...

Side-Align Your List in HTML

Presenting this image: https://i.sstatic.net/VNF7J.png However, I desire it to be like this: https://i.sstatic.net/ItgEN.png Don't mind the white space, it's just decoration. .server_name { font-size: 50; padding-left: 5; ...

Enhanced jQuery Pop-Up Panel

A few issues to address: 1) My goal is to optimize the efficiency of this script. 2) When a user clicks on either pop-out button, it opens a window and hides the element. (Currently using .detach() to remove the embedded video player because in Firefox . ...

Modify MUI's ListItemText component by targeting a specific span to implement customized styles using the useStyle hook

It's quite perplexing that although this is a straightforward task in regular CSS, it must be accomplished through MUI's useStyles for some peculiar reason. Essentially, I possess a ListItem containing a ListItemText. It appears as follows: cons ...

What is the best way to utilize jspdf for formatting data, specifically when wanting the first column to be in bold?

How can I customize data formatting using jspdf? Specifically, I would like the first column to be in bold and the second column in normal text. Additionally, I want to align them in the middle of the pdf output with different colors for each column. Belo ...

Troubleshooting FileReader.onload Failure Following Form Submission (POST) in JavaScript

Currently, I am in the process of developing a single-page uploader using Javascript (+jQuery) and leveraging AppJS. This uploader consists of two forms with upload inputs. Both forms are on the same page, with the second form initially hidden using displ ...

Message toggle not functioning in custom method of jQuery Validate plugin

Here is a custom rule that I have implemented: jQuery.validator.addMethod("onespecial", function(value, element){ var pattern = /^(?=.{6,})(?=.*[a-z])(?=.*[0-9])|(?=.*[!@#$%^&*()-+=]).*$/; return (pattern.test(value)); }, "Your ...

Passing data from jQuery to FPDFI need to transfer variables

I am facing an issue while trying to send form data using jQuery to FPDF. The pdf generated is blank and does not display the variables even though there are no errors. I have simplified the code to include only one variable for clarity. Any suggestions on ...

Chart.js outdated data not being cleared

This query has been repeated numerous times, and despite my efforts to find a solution in previous discussions, I have not been successful. As part of the reporting feature, I am creating multiple bar charts using a for loop. I am utilizing node.js with ...