Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Javascript resets my existing CSS alignment

Below is the some javascript button I've written to hide/show the table. By default table is centered with margin: auto, and it works.

But, whenever I click the button to hide/show table, it resets the auto margins, and left-aligns the table.

If I disable the display:none, it shows table centered, and if I remove the javascript it displays it normally. I've tried setting it again with

document.getElementById("vcontrols").style.margin = "0 auto"

But it doesn't work

Here's the CSS used.

    <style>
        body {
            background-color: #111;
        }

        .container {
            max-width: 80%;
            margin: auto;
            background: #333;
            color: #fff;
            text-align: center;
        }

        .tabela {
            margin: 0 auto;
        }

        #vcontrols {
            display: none;
        }
</style>

Here's the script

 function toggleControls() {
    var Skrivalica = document.getElementById('vcontrols');
    var displaySetting = Skrivalica.style.display;
    var hideshow = document.getElementById('hideshow');

    if (displaySetting == 'block') {
      Skrivalica.style.display = 'none';
      hideshow.innerHTML = 'Full Details';
    }
    else {
      Skrivalica.style.display = 'block';
      hideshow.innerHTML = 'Hide Details';
    }
  }

And Here's the main body, table

        <div class="container">
        <button onclick="toggleControls()" id="hideshow">Full Details</button>
        <table class="tabela" id="vcontrols">
            <tr>
                <th>Action 1</th>
                <th>Action 2</th>
                <th>Action 3</th>
                <th>Action 4</th>
                <th>Action 5</th>
            </tr>
            <tr>
                <td>Item 1</td>
                <td>Item 2</td>
                <td>Item 3</td>
                <td>Item 4</td>
                <td>Item 5</td>
            </tr>
        </table>
    </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function toggleControls() {
    var Skrivalica = document.getElementById('vcontrols');
    var displaySetting = Skrivalica.style.display;
    var hideshow = document.getElementById('hideshow');
    
    if (displaySetting === 'block') {
      Skrivalica.style.display = 'none';
      hideshow.textContent = 'Full Details';
    }
    else {
      Skrivalica.style.display = 'block';
      hideshow.textContent = 'Hide Details';
    }
}
    body {
        background-color: #111;

    }
    
    .btn-container {
        display: flex;
        justify-content: center;
    }

    .container {
        max-width: 80%;
        margin-left: auto;
        margin-right: auto;
        background: #333;
        color: #fff;
        text-align: center;
        display: flex;
    }

    .tabela {
        margin: 0 auto;
    }

    #vcontrols {
        display: none;
    }
<div class="btn-container">
  <button onclick="toggleControls()" id="hideshow">Full Details</button>
  </div>
  <div class="container">
    <table class="tabela" id="vcontrols">
        <tr>
            <th>Action 1</th>
            <th>Action 2</th>
            <th>Action 3</th>
            <th>Action 4</th>
            <th>Action 5</th>
        </tr>
        <tr>
            <td>Item 1</td>
            <td>Item 2</td>
            <td>Item 3</td>
            <td>Item 4</td>
            <td>Item 5</td>
        </tr>
    </table>
</div>

Maybe an easier way but I changed up some HTML and CSS. Left the JS alone. Seemed to be working for me. Let me know if its not what you wanted!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!